#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
long long s[N];
long long cost(int i,int l,int r)
{
if(l>r) return 0;
return 1ll*a[i]*(i-l)-(s[i-1]-s[l-1])+(s[r]-s[i])-1ll*a[i]*(r-i);
}
int besthub(int R,int L,int X[],long long B)
{
for(int i=1;i<=R;i++)
{
a[i]=X[i-1];
s[i]=s[i-1]+a[i];
}
int ans=0;
for(int i=1;i<=R;i++)
{
int pos=a[i];
int l=i,r=R,res=-1;
while(l<=r)
{
int mid=(l+r)/2;
int v=mid;
int need=pos-(a[mid]-pos);
int u=upper_bound(a+1,a+R+1,need)-a;
if(cost(i,u,v)<=B)
{
res=mid;
l=mid+1;
}
else r=mid-1;
}
int need=pos-(a[res]-pos);
int u=upper_bound(a+1,a+R+1,need)-a;
long long cur=B-cost(i,u,res);
l=1,r=u;
int id;
while(l<=r)
{
int mid=(l+r)/2;
if(1ll*pos*(u-mid)-(s[u-1]-s[mid-1])>cur) l=mid+1;
else
{
id=mid;
r=mid-1;
}
}
ans=max(ans,res-id+1);
}
return ans;
}
/*
int main()
{
int R,L,B;
cin>>R>>L>>B;
int pos[R];
for(int i=0;i<R;i++) cin>>pos[i];
cout<<besthub(R,L,pos,B);
}
5 20 6
1
2
10
12
14
*/
Compilation message
ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:55:24: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | ans=max(ans,res-id+1);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
640 KB |
Output is correct |
2 |
Correct |
25 ms |
640 KB |
Output is correct |
3 |
Correct |
111 ms |
1936 KB |
Output is correct |
4 |
Correct |
108 ms |
2944 KB |
Output is correct |
5 |
Correct |
35 ms |
1408 KB |
Output is correct |
6 |
Correct |
34 ms |
1408 KB |
Output is correct |
7 |
Correct |
67 ms |
2688 KB |
Output is correct |
8 |
Correct |
72 ms |
2688 KB |
Output is correct |
9 |
Correct |
53 ms |
1440 KB |
Output is correct |
10 |
Correct |
48 ms |
1408 KB |
Output is correct |
11 |
Correct |
93 ms |
2944 KB |
Output is correct |
12 |
Correct |
114 ms |
2936 KB |
Output is correct |
13 |
Correct |
57 ms |
1536 KB |
Output is correct |
14 |
Correct |
61 ms |
1444 KB |
Output is correct |
15 |
Correct |
67 ms |
2304 KB |
Output is correct |
16 |
Correct |
72 ms |
2304 KB |
Output is correct |
17 |
Correct |
81 ms |
2688 KB |
Output is correct |
18 |
Correct |
89 ms |
2712 KB |
Output is correct |
19 |
Correct |
109 ms |
2948 KB |
Output is correct |
20 |
Correct |
99 ms |
2936 KB |
Output is correct |