#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
int besthub(int R, int L, int X[], long long B)
{
int low = 0;
int high = R*2;
while(low < high){
int mid = (high+low)/2;
// cout << mid << " " << low << " " << high << endl;
long long sum = 0;
long long now = X[mid/2];
long long pluss = mid - mid/2 - 1;
long long mines = mid - (pluss + 1);
// cout << "lenth " << mid << endl;
// cout << now << " << now " << pluss << " << pluse " << mines << " << mines " << endl;
for (int i = 0 ; i < mid ; i++){
sum+= llabs(now - X[i]);
}
// cout << "first sum " << sum << endl;
// cout << "B" << B << endl;
if (sum <= B){
low = mid + 1;
continue;
}
int good = false;
for (int i = mid/2 + 1 ; i + pluss < R ; i++){
int bye = X[i - mines - 1];
// cout << "bye " << bye << endl;
sum-= llabs(X[i-1] - bye);
// cout << "sum without bye " << sum << endl;
int hello = X[i + pluss];
// cout << "hello " << hello << endl;
sum+= llabs(X[i] - hello);
// cout << "sum with hello " << sum << endl;
sum+= llabs(X[i] - X[i-1])*(mines - 1);
// cout << "sum with pre" << sum << endl;
sum-= llabs(X[i] - X[i-1])*(pluss - 1);
// cout << "sum without after" << sum << endl;
// cout << "new sum " << sum << endl;
if (sum <= B){
low = mid + 1;
good = true;
break;
}
}
if (good)continue;
high = mid - 1;
}
int best = low - 1;
// for (int i = max(low - 5, 0) ; i <= min(low + 5, R) ; i++){
// long long sum = 0;
//
// long long now = X[i/2];
//
// long long pluss = i - i/2 - 1;
//
// long long mines = i - pluss + 1;
//
// cout << "for " << i << endl;
//
// cout << "sums are :\n";
//
//
// for (int j = 0 ; j < i ; j++){
// sum+= llabs(now - X[j]);
// }
//
// cout << sum << endl;
//
// if (sum <= B){
// best = i;
// continue;
// }
//
// int good = false;
//
//
// for (int j = now + 1 ; j + pluss - 1 < R ; j++){
// int bye = X[j - mines];
// sum-= llabs(X[j-1] - bye);
// int hello = X[j + pluss];
// sum+= llabs(X[j] - hello);
//
// sum+= llabs(X[j] - X[j-1])*mines;
//
// sum-= llabs(X[j] - X[j-1])*(pluss - 1);
//
// cout << sum << endl;
//
// if (sum <= B){
// best = i;
// good = true;
// break;
// }
//
// }
//
// if (good)continue;
// }
return best;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Incorrect |
20 ms |
1664 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |