#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];
}
/*
for(int i : pref){
cout << i << " ";
}
cout << endl;
*/
int l = 1;
int final = 1;
for(int r =1; r <= n;r++){
int m = (l+r)/2;
// 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;
if(((x[m-1]*(m-l)) - (pref[m-1]-pref[l-1]) + ((pref[r]-pref[m]) - (x[m-1]*(r-m))))<= b){
final = max(final,r-l+1);
}
}
}
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;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
440 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 |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
448 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
1 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 |
1 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 |
392 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
448 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 |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
2652 KB |
Output is correct |
4 |
Incorrect |
9 ms |
2652 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |