Skip to main content

Posts

Showing posts from September, 2018

SVADA - Svada

Problem Link #include <iostream> #include <cmath> using namespace std; typedef long long ll; int n, m; ll a[101], b[101], c[101], d[101]; ll calculateCoconutsProduced(ll time) { ll coconutsProduced = 0; for(int i = 0; i < n; i++) { coconutsProduced += (time - a[i] >= 0) ? (time - a[i]) / b[i] + 1 : 0; } return coconutsProduced; } ll calculateCoconutsConsumed(ll time) { ll coconutsConsumed = 0; for(int i = 0; i < m; i++) { coconutsConsumed += (time - c[i] >= 0) ? (time - c[i]) / d[i] + 1 : 0; } return coconutsConsumed; } void solve(ll s, ll e) { ll endTime = e; while(s < e) { ll mid = s + (e - s + 1) / 2; ll coconutsProduced = calculateCoconutsProduced(mid); ll coconutsConsumed = calculateCoconutsConsumed(endTime - mid); if(coconutsProduced > coconutsConsumed) { e = mid - 1; } else { s = mid; } } cout << s << endl; } int main() { ll t; cin >

MKUHAR - Most Servings Meal

Problem Link #include <iostream> #include <cmath> using namespace std; struct node { int x, y, pm, sm, pv, sv; node(){} node(int x, int y, int pm, int sm, int pv, int sv) { this->x = x; this->y = y; this->pm = pm; this->sm = sm; this->pv = pv; this->sv = sv; } }; int n; node arr[101]; /* Find out if it is possible to have given number of servings for each ingredient with given money */ bool isServingPossible(int totalServings, int money) { int required = 0; /* check if we can have totalServings of each ingredient */ for(int i = 0; i < n; i++) { required = totalServings * arr[i].x; required -= arr[i].y; int minMoneySpent = 99999999; int limit = ceil(required / (double)arr[i].sm); for(int numSmallItem = 0; numSmallItem <= limit; numSmallItem++) { int totalSmallItem = numSmallItem * arr[i].sm; int numLargeItem = (required > totalSmallItem)? ceil((required - totalSmal

PIE - Pie

Problem Link #include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int n, f; double radius[10001]; bool isVolumePossible(double p) { int total = 0; for(int i = 0; i < n; i++) { total += (int)floor((M_PI * radius[i]) / p); } if(total >= f + 1) return true; return false; } void solve(double s, double e) { double result = 0.0; for(int i = 0; i < 60; i++) { double mid =  (e + s) / 2.0; if(isVolumePossible(mid)) { s = mid; result = mid; } else { e = mid; } } printf("%.4lf\n", result); } int main() { double maxVolume = 0; int t; cin >> t; while(t--) { cin >> n >> f; memset(radius, 0, sizeof radius); for(int i = 0; i < n; i++) { cin >> radius[i]; radius[i] *= radius[i]; } maxVolume = 4e8; solve(0.0, maxVolume); } return 0; }

RENT - Rent your airplane and make money

Problem Link #include <iostream> #include <algorithm> using namespace std; typedef long long ll; struct Order { ll s; ll e; ll p; Order(){} Order(ll s, ll e, ll p) { this->s = s; this->e = e; this->p = p; } bool operator <(const Order &other) const { return e < other.e; } }; int findLastNonConflictingOrder(Order arr[], int index) { int start = 0; int end = index -1; int result = -1; while(start <= end) { int mid = (start + end) / 2; if(arr[index].s >= arr[mid].e) { result = mid; start = mid + 1; } else { end = mid - 1; } } return result; } void findMaxProfit(Order arr[], int n) { ll dp[n]; dp[0] = arr[0].p; ll profitInclusive, profitExclusive; for(int i = 1; i < n; i++) { profitInclusive = arr[i].p; int j = findLastNonConflictingOrder(arr, i); if(j != -1) { profitInclusive += dp[j]; } profitExclusive = dp[i - 1]

SUBSUMS - Subset Sums

Problem Link #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; void computeSubsetSum(long long arr[], long long result[], int start, int len) { for(int i = 0; i < (1 << len); i++) { for(int j = 0; j < len; j++) { if(i & (1 << j)) { result[i] += arr[start + j]; } } } } int main() { int n; long long a, b; long long arr[35]; cin >> n >> a >> b; int size_lhs = 1 << (n / 2); int size_rhs = 1 << (n - n / 2); long long lhs[size_lhs]; long long rhs[size_rhs]; memset(lhs, 0, sizeof lhs); memset(rhs, 0, sizeof rhs); for(int i = 0; i < n; i++) { cin >> arr[i]; }     /* Divide the set in 2 part n/ 2 element each        and compute sum of subsets for both the part        2^(n/2) + 2^(n / 2) < 2 ^n     */     computeSubsetSum(arr, lhs, 0, n / 2); computeSubsetSum(arr, rhs,

SCALE - Funny scales

Problem Link #include <iostream> #include <algorithm> #include <string.h> #include <cmath> using namespace std; const int N = 1e4; int rhs[N]; int lhs[N]; int convertToTernary(long long x, int arr[], int index) {   if(x == 0)   return index;   int remainder = x % 3;   arr[index] = remainder;   x = x / 3;   return convertToTernary(x, arr, index + 1); } long convertToDecimal(int arr[], int len) { long result = 0; long power = 1; int i = 0; while(i < len) { result += arr[i] * power; power *= 3; i++; } return result; } void solve(int arr[],  int len, long maxSum) { int i = 0; int carry = 0; int rlen = 0; while(i < len) { arr[i] += carry; carry = arr[i] / 3; arr[i] = arr[i] % 3; if(arr[i] == 0 || arr[i] == 1) { lhs[i] = 0; } else { lhs[i] = 1; carry = 1; } rhs[i] = (arr[i] + lhs[i]) % 3; i++; } rlen = len; if(carry) { rhs[i] = carry;

NOTATRI

Problem Link #include <iostream> #include <vector> #include <algorithm> using namespace std; long arr[2002]; int binarySearch(int start, int end, int x){ while(end > start){ int mid = (start + end) / 2; if(arr[mid] > x){ end = mid; } else{ start = mid + 1; } } return end; } int main(){ int n, b; long result = 0; while(true){ cin >> n; if(n == 0) break; for(int i = 0; i < n; i++){ cin >> arr[i]; } sort(arr, arr + n); result = 0; for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ if(arr[i] + arr[j] < arr[n - 1]){   /* Find the first element which is greater than sum of two element */ b = binarySearch(i, n - 1, arr[i] + arr[j]); result += (n - b); } } } cout << result << endl; } return 0; }

ABCDEF

Problem Link #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int arr[105]; int n, a, b, c, d, e, f, g; vector<long> lhs; // lhs stores result of a * b + c vector<long> rhs; // rhs stores result of d * (e + f) cin >> n; for(int i = 0; i < n; i++){ cin >> arr[i]; } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ for(int k = 0; k < n; k++){ a = arr[i]; b = arr[j]; c = arr[k]; d = arr[i]; e = arr[j]; f = arr[k]; lhs.push_back((a * b) + c); if(d != 0){ rhs.push_back(d * (e + f)); } } } } sort(lhs.begin(), lhs.end()); sort(rhs.begin(), rhs.end()); vector<long>::iterator it, l1, l2, u1, u2; it = lhs.begin(); long result = 0; while(it != lhs.end()){ l1 = lower_bound(lhs.begin(), lhs.end(), *it); u1 = upper_bound(lhs.begin(), lhs.end(), *it); l2 = lower_b

Merge Sort: MRGSRT

Problem Link #include <iostream> using namespace std; int printIntermediateSteps(int s, int t, int i){ if(s > t){ return 0; } if(s == t){ return 1; } int mid = (s + t) / 2; if(i <= mid){ cout << s << " " << mid << endl; return printIntermediateSteps(s, mid, i) + 1; } else{ cout << (mid + 1) << " " << t << endl; return printIntermediateSteps(mid + 1, t, i) + 1; } } int main(){ int t, s, T, i, depth; cin >> T; while(T--){ cin >> s >> t >> i; if(i < s || i > t){ cout << "-1"; continue; } depth = 1; cout << s << " " << t << endl; depth = printIntermediateSteps(s, t, i); cout << depth << endl; } return 0; }