#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//return number of rice fields
vector<ll> s;
vector<ll> coord;
ll totalcostrtol(int hub, int e){//e hub
assert(e>=hub);
return s[e]-s[hub]-((e-hub)*coord[hub]);
}
ll totalcostltor(int hub, int e){//hub e
assert(e<=hub);
return coord[hub]*(hub-e+1)-(s[hub]-(e==0?0:s[e-1]));
}
int n;
ll success(int ind, ll range, ll b){
//returns max truckloads of rice, or -1 if not possible
//using all rice up to and including coord (plus a bit more if necessary)
//cout<<"check "<<ind<<" "<<range<<": ";
ll total = 0;
ll ricehubs = 0;
int left = lower_bound(coord.begin(), coord.end(), coord[ind]-range)-coord.begin();
//cout<<left<<" ";
total+=totalcostltor(ind,left);
int right = upper_bound(coord.begin(), coord.end(), coord[ind]+range)-coord.begin()-1;
//cout<<right<<'\n';
total+=totalcostrtol(ind,right);
//cout<<total<<'\n';
if (total>b){
//cout<<"fail"<<'\n';
return -1;
}
ricehubs+=right-left+1;
if (left-1>=0){
int left2 = lower_bound(coord.begin(), coord.end(), coord[left-1])-coord.begin();
ll numadd = (min(b-total,ll((left-left2)*(coord[ind]-coord[left-1])))/(coord[ind]-coord[left-1]));
ricehubs+=numadd;
total+=numadd*(coord[ind]-coord[left-1]);
}
if (right+1<n){
int right2 = upper_bound(coord.begin(), coord.end(), coord[right+1])-coord.begin()-1;
ricehubs+=(min(b-total,ll((right2-right)*(coord[right+1]-coord[ind])))/(coord[right+1]-coord[ind]));
}
return ricehubs;
}
int besthub(int R, int L, int X[], long long B){
ll maxfields = 0;
//coords from 1 to L
n = R;
coord = vector<ll>(X,X+n);
s.resize(n);
s[0]=coord[0];
for (int i = 1; i<n; i++){
s[i]=s[i-1]+coord[i];
}
for (int i = 0; i<n; i++){
//cout<<"consider "<<i<<'\n';
//consider rice field i
ll numfields = 1;
//go left
ll l=0,r=max(coord[n-1]-coord[i],coord[i]-1)+10;
while (l<=r){
ll mid = (l+r)/2;
if (success(i, mid, B)!=-1){
l=mid+1;
} else {
r=mid-1;
}
}
//l is leftmost hub possible
ll sc = success(i,l-1,B);
//cout<<sc<<'\n';
maxfields=max(maxfields,sc);
}
return maxfields;
}
Compilation message
ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:65:6: warning: unused variable 'numfields' [-Wunused-variable]
ll numfields = 1;
^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
376 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 |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
384 KB |
Output is correct |
24 |
Correct |
8 ms |
512 KB |
Output is correct |
25 |
Correct |
10 ms |
512 KB |
Output is correct |
26 |
Correct |
16 ms |
512 KB |
Output is correct |
27 |
Correct |
13 ms |
512 KB |
Output is correct |
28 |
Correct |
13 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
768 KB |
Output is correct |
2 |
Correct |
57 ms |
896 KB |
Output is correct |
3 |
Correct |
323 ms |
3452 KB |
Output is correct |
4 |
Correct |
327 ms |
3320 KB |
Output is correct |
5 |
Correct |
76 ms |
1664 KB |
Output is correct |
6 |
Correct |
76 ms |
1664 KB |
Output is correct |
7 |
Correct |
196 ms |
3072 KB |
Output is correct |
8 |
Correct |
194 ms |
3072 KB |
Output is correct |
9 |
Correct |
119 ms |
1656 KB |
Output is correct |
10 |
Correct |
114 ms |
1656 KB |
Output is correct |
11 |
Correct |
321 ms |
3320 KB |
Output is correct |
12 |
Correct |
318 ms |
3328 KB |
Output is correct |
13 |
Correct |
161 ms |
1760 KB |
Output is correct |
14 |
Correct |
158 ms |
1664 KB |
Output is correct |
15 |
Correct |
240 ms |
2560 KB |
Output is correct |
16 |
Correct |
240 ms |
2680 KB |
Output is correct |
17 |
Correct |
291 ms |
3072 KB |
Output is correct |
18 |
Correct |
289 ms |
3192 KB |
Output is correct |
19 |
Correct |
318 ms |
3200 KB |
Output is correct |
20 |
Correct |
314 ms |
3268 KB |
Output is correct |