#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
6 ms |
372 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
768 KB |
Output is correct |
3 |
Correct |
23 ms |
2552 KB |
Output is correct |
4 |
Correct |
28 ms |
2560 KB |
Output is correct |
5 |
Correct |
16 ms |
1280 KB |
Output is correct |
6 |
Correct |
16 ms |
1280 KB |
Output is correct |
7 |
Correct |
21 ms |
2304 KB |
Output is correct |
8 |
Correct |
25 ms |
2304 KB |
Output is correct |
9 |
Correct |
18 ms |
1152 KB |
Output is correct |
10 |
Correct |
18 ms |
1152 KB |
Output is correct |
11 |
Correct |
26 ms |
2560 KB |
Output is correct |
12 |
Correct |
29 ms |
2560 KB |
Output is correct |
13 |
Correct |
18 ms |
1280 KB |
Output is correct |
14 |
Correct |
19 ms |
1280 KB |
Output is correct |
15 |
Correct |
24 ms |
2048 KB |
Output is correct |
16 |
Correct |
23 ms |
1920 KB |
Output is correct |
17 |
Correct |
22 ms |
2304 KB |
Output is correct |
18 |
Correct |
26 ms |
2304 KB |
Output is correct |
19 |
Correct |
23 ms |
2432 KB |
Output is correct |
20 |
Correct |
27 ms |
2432 KB |
Output is correct |