#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n=c.size(),m=v.size();
vector<long long> S(m+1),Smin(m+1),Smax(m+1),SS(m),SSmin(m),SSmax(m);
for(int i=m-1;i>=0;i--) {
S[i]=S[i+1]+v[i];
Smin[i]=min(Smin[i+1],S[i]);
Smax[i]=max(Smax[i+1],S[i]);
}
SS[0]=v[0];
for(int i=1;i<m;i++)
SS[i]=SS[i-1]+v[i];
SSmin.back()=SSmax.back()=SS.back();
for(int i=m-2;i>=0;i--) {
SSmin[i]=min(SSmin[i+1],SS[i]);
SSmax[i]=max(SSmax[i+1],SS[i]);
}
vector<int> rs(n);
for(int i=0;i<n;i++) {
int l=0,r=m-1,pivot=-1;
while(l<=r) {
int mid=(l+r)/2;
if(Smax[mid]-Smin[mid]>c[i]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
// Find the last time touching the upper wall
if(pivot!=-1&&Smax[pivot]==S[pivot]) {
l=pivot+1,r=m;
while(l<=r) {
int mid=(l+r)/2;
if(Smin[mid]==Smin[pivot]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
l=pivot,r=m-1;
int limit=SS[pivot];
while(l<=r) {
int mid=(l+r)/2;
if(SSmax[mid]>limit) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
rs[i]=c[i]+S[pivot];
} else { // Find the last time touching the lower wall
l=pivot,r=m;
while(l<=r) {
int mid=(l+r)/2;
if(Smax[mid]==Smax[pivot]) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
l=pivot,r=m-1;
int limit=SS[pivot];
while(l<=r) {
int mid=(l+r)/2;
if(SSmin[mid]<limit) {
pivot=mid;
l=mid+1;
} else
r=mid-1;
}
rs[i]=S[pivot];
}
}
return rs;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
167 ms |
16716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |