Submission #336042

#TimeUsernameProblemLanguageResultExecution timeMemory
336042aryan12Rice Hub (IOI11_ricehub)C++17
0 / 100
4 ms876 KiB
#include <bits/stdc++.h> #include "ricehub.h" using namespace std; int besthub(int r, int l, int x[], long long b) { long long pre[r + 1], suf[r + 1]; pre[0] = 0; pre[1] = 0; for(long long i = 2; i <= r; i++) { pre[i] = pre[i - 1] + (x[i] - x[i - 1]) * (i - 1); } suf[r] = 0; for(long long i = r - 1; i >= 1; i--) { suf[i] = suf[i + 1] + (x[i + 1] - x[i]) * (r - i); } /*for(long long i = 1; i <= r; i++) { cout << pre[i] << " "; } cout << endl; for(long long i = 1; i <= r; i++) { cout << suf[i] << " "; } cout << endl;*/ long long ans = 1; for(long long i = 2; i <= r; i++) { long long left = 1, right = i, best = r; while(left <= right) { long long LeftPoint = (left + right) / 2; long long OptimalField = (LeftPoint + i) / 2; long long cost = 0; cost = pre[OptimalField] + suf[OptimalField]; cost -= (pre[LeftPoint] + ((x[OptimalField] - x[LeftPoint]) * (LeftPoint - 1))); cost -= (suf[i] + ((x[i] - x[OptimalField]) * (r - i))); /*for(long long j = LeftPoint; j <= i; j++) { cost += abs(x[j] - x[OptimalField]); }*/ //cout << "i = " << i; //cout << ", left = " << left << ", right = " << right << ", LeftPoint = " << LeftPoint << ", Optimal Field = " << OptimalField << endl; if(cost <= b) { right = LeftPoint - 1; best = LeftPoint; } else { left = LeftPoint + 1; } } //cout << i << " " << best << endl; ans = max(ans, i - best + 1); } int lol = ans; return lol; } /*int main() { //cout << "Input the number of rice fields" << endl; int r; cin >> r; //cout << "Input the maximum coordinate" << endl; int l; cin >> l; //cout << "Input r numbers, denoting the rice fields" << endl; int x[r + 1]; for(int i = 1; i <= r; i++) { cin >> x[i]; } //cout << "Input the budget" << endl; int b; cin >> b; cout << besthub(r, l, x, b) << endl; }*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...