This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "ricehub.h"
using namespace std;
#define ll long long
int besthub(int R, int L, int X[], long long B) {
int ans=1;
ll cost=0;
ll pref[R];
for (ll i=0; i<R; i++){
if (!i)
pref[i] = X[i];
else
pref[i] = pref[i-1] + X[i];
}
ll l=R-1;
for (int i=R-2; ~i; i--){
if (cost + (ll)(X[R-1] - X[i]) <= B){
l--;
cost+=(ll)(X[R-1] - X[i]);
ans++;
}else break;
}
for (ll i=R-2; ~i; i--){
ll dist=X[i+1] - X[i];
if (l>i){
l=i;
}else{
cost -= dist * (i+1-l);
}
ll lo=i, hi=R;
while(lo+1!=hi){
ll en=(lo + hi)/2;
ll cost2=pref[en] - pref[i] - (en-i)*(ll)X[i];
if (cost + cost2 <= B)
lo=en;
else
hi=en;
}
int currans=lo-l+1;
ll currcost = cost + pref[lo] - pref[i] - (lo-i)*(ll)(X[i]);
int newl=l;
//bool ok=0;
ll auxcost=cost;
//while(1){
//if (ok) break;
if (newl - 1 >= 0){
while(1){
if (newl - 1 >= 0 && auxcost + (X[i] - X[newl-1]) <= B){
newl--;
auxcost += (ll)(X[i] - X[newl]);
ll lo1=i, hi1=R;
while(lo1+1!=hi1){
ll en=(lo1 + hi1)/2;
ll cost2=pref[en] - pref[i] - (en-i)*(ll)X[i];
if (auxcost + cost2 <= B)
lo1=en;
else
hi1=en;
}
ll cans = lo1 - newl + 1;
ll ccost= auxcost + pref[lo1] - pref[i] - (lo1-i)*(ll)(X[i]);
if (cans == currans && ccost <= currcost){
currcost = ccost;
l=newl;
cost = auxcost;
}else if (cans > currans){
currcost=ccost;
currans = cans;
l=newl;
cost=auxcost;
}else{
break;
}
}else{
break;
}
}
}
//}
ans=max(ans, currans);
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |