#include <bits/stdc++.h>
#include "ricehub.h"
using namespace std;
int besthub(int r, int l, int x[], long long b) {
int a[r + 1];
for(int i = 0; i < r; i++) {
a[i + 1] = x[i];
}
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] + (a[i] - a[i - 1]) * (i - 1);
}
suf[r] = 0;
for(long long i = r - 1; i >= 1; i--) {
suf[i] = suf[i + 1] + (a[i + 1] - a[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 = pre[OptimalField], cost1 = suf[OptimalField];
cost -= (pre[LeftPoint] + ((a[OptimalField] - a[LeftPoint]) * (LeftPoint - 1)));
cost1 -= (suf[i] + ((a[i] - a[OptimalField]) * (r - i)));
cost = abs(cost);
cost1 = abs(cost);
cost += cost1;
/*for(long long j = LeftPoint; j <= i; j++) {
cost += abs(a[j] - a[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 maaimum coordinate" << endl;
int l;
cin >> l;
//cout << "Input r numbers, denoting the rice fields" << endl;
int a[r + 1];
for(int i = 1; i <= r; i++) {
cin >> a[i];
}
//cout << "Input the budget" << endl;
int b;
cin >> b;
cout << besthub(r, l, a, b) << endl;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
748 KB |
Output is correct |
2 |
Incorrect |
4 ms |
748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |