#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pf push_front
#define pb push_back
#define pi pair<int,int>
#define vi vector<int>
/*
int besthub(int n, int last, int x[], ll b){
int ans = 0;
int l , r;
for(int i = 0; i < n; i++){
l = i - (lower_bound(x.begin(), x.end(), max(0LL,x[i]-b))- x.begin());
r = upper_bound(x.begin(), x.end(), min((ll) last, x[i]+b)) - x.begin() - (i+1);
// cout << x[i] << " " <<l << " " << r << endl;
ans = max(l+r+1,ans);
}
return ans;
}
*/
int besthub(int n, int last, int x[], ll b){
ll ans = 0;
ll cnt;
ll pref[n+1];
pref[0] = 0;
for(int i = 1; i <= n; i++){
pref[i] = pref[i-1]+x[i-1];
}
if(b>=pref[n]){
return n;
}
ll l = 1;
ll final = 1;
for(ll r =1; r <= n;r++){
ll m = (l+r)/2;
if(final==n) return n;
// ll val = pref[r] - pref[m] + (pref[m]-pref[max(0,l)]);
ll val= ((x[m-1]*(m-l)) - (pref[m-1]-pref[l-1]) + ((pref[r]-pref[m]) - (x[m-1]*(r-m))));
//cout <<l << " "<< r << " " << val<<endl;
// cout << val << endl;
if( ((x[m-1]*(m-l)) - (pref[m-1]-pref[l-1]) + ((pref[r]-pref[m]) - (x[m-1]*(r-m))))<= b){
// cout << l << " " << r << endl;
final = max(final,r-l+1);
}
else{
while(((x[m-1]*(m-l)) - (pref[m-1]-pref[l-1]) + ((pref[r]-pref[m]) - (x[m-1]*(r-m)))) > b && l<r){
m = (l+r)/2;
l++;
}
m = (l+r)/2;
}
}
return final;
}
Compilation message
ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:59:11: warning: unused variable 'val' [-Wunused-variable]
59 | ll val= ((x[m-1]*(m-l)) - (pref[m-1]-pref[l-1]) + ((pref[r]-pref[m]) - (x[m-1]*(r-m))));
| ^~~
ricehub.cpp:34:8: warning: unused variable 'ans' [-Wunused-variable]
34 | ll ans = 0;
| ^~~
ricehub.cpp:36:8: warning: unused variable 'cnt' [-Wunused-variable]
36 | ll cnt;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
448 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
484 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
2612 KB |
Output is correct |
4 |
Correct |
9 ms |
2652 KB |
Output is correct |
5 |
Correct |
4 ms |
1360 KB |
Output is correct |
6 |
Correct |
4 ms |
1372 KB |
Output is correct |
7 |
Correct |
6 ms |
2140 KB |
Output is correct |
8 |
Correct |
9 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
4 ms |
1320 KB |
Output is correct |
11 |
Correct |
8 ms |
2480 KB |
Output is correct |
12 |
Correct |
9 ms |
2652 KB |
Output is correct |
13 |
Correct |
4 ms |
1372 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
1972 KB |
Output is correct |
17 |
Correct |
8 ms |
2392 KB |
Output is correct |
18 |
Correct |
6 ms |
2224 KB |
Output is correct |
19 |
Correct |
8 ms |
2396 KB |
Output is correct |
20 |
Correct |
8 ms |
2396 KB |
Output is correct |