#include<bits/stdc++.h>
#define MAXN 1007
using namespace std;
const long long inf=1e15;
int n;
long long pref[MAXN],suff[MAXN],sum[MAXN],dist[MAXN];
long long curr,ans,c,d1,d2,e,w[MAXN];
multiset<long long> s;
multiset<long long>::iterator it;
long long getmax(){
if(s.empty())return 0;
it=s.end(); it--;
return *it;
}
void dfs(int x,int p,int l,int r,long long lim,long long s){
e=max(e,s+dist[x]);
if(x==l and p!=r and s+c<=lim)dfs(r,l,l,r,lim,s+c);
if(x==r and p!=l and s+c<=lim)dfs(l,r,l,r,lim,s+c);
if(x<r and x+1!=p and s+sum[x+1]-sum[x]<=lim)dfs(x+1,x,l,r,lim,s+sum[x+1]-sum[x]);
if(x>l and x-1!=p and s+sum[x]-sum[x-1]<=lim)dfs(x-1,x,l,r,lim,s+sum[x]-sum[x-1]);
}
long long cycle(int from,int to){
long long ss=sum[to]-sum[from]+c,res=0,len=0,torem=0;
int pt=from;
s.clear();
for(int i=from;i<=to;i++){
w[i]=dist[i];
s.insert(w[i]);
while(true){
if(pt>=i and pt<to and sum[pt+1]-sum[i]<=ss/2){len=sum[pt+1]-sum[i]; pt++; w[pt]=len+dist[pt]+torem; s.insert(w[pt]);}
else if(pt==to and sum[to]-sum[i]+c<=ss/2){len=sum[to]-sum[i]+c; pt=from; w[pt]=len+dist[pt]+torem; s.insert(w[pt]);}
else if(pt<i and sum[to]-sum[i]+c+sum[pt+1]-sum[from]<=ss/2){len=sum[to]-sum[i]+c+sum[pt+1]-sum[from]; pt++; w[pt]=len+dist[pt]+torem; s.insert(w[pt]);}
else break;
}
if(!s.empty()){
s.erase(s.find(w[i]));
}
res=max(res,getmax()-torem)+dist[i];
torem+=sum[i+1]-sum[i];
}
e=0; dfs(from,0,from,to,ss/2,0); d1=e;
e=0; dfs(to,0,from,to,ss/2,0); d2=e;
return res;
}
long long find_shortcut(int N,vector<int> l,vector<int> d, int C){
n=N; c=C;
for(int i=1;i<=n;i++){
dist[i]=d[i-1];
}
pref[1]=d[0]; sum[1]=0;
for(int i=0;i<l.size();i++){
pref[i+2]=max(pref[i+1]+l[i],d[i+1]*1LL);
sum[i+2]=sum[i+1]+l[i];
}
suff[n]=d[n-1];
for(int i=l.size()-1;i>=0;i--){
suff[i+1]=max(suff[i+2]+l[i],d[i]*1LL);
}
ans=inf;
for(int i=1;i<=n;i++){
for(int f=i+1;f<=n;f++){
if(sum[f]-sum[i]<=c)continue;
curr=pref[i]+c+suff[f];
curr=max(curr,cycle(i,f));
curr=max(curr,max(d1+pref[i],d2+suff[f]));
//cout<<i<<" "<<f<<" "<<d1<<" "<<d2<<" "<<curr<<"\n";
ans=min(ans,curr);
}
}
return ans;
}
/*
int main(){
//cout<<find_shortcut(4, {10, 20, 20}, {0, 40, 0, 30}, 10)<<"\n";
//cout<<find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10}, {20, 0, 30, 0, 0, 40, 0, 40, 0}, 30)<<"\n";
cout<<find_shortcut(4, {2, 2, 2}, {1, 10, 10, 1}, 1)<<"\n";
}
*/
Compilation message
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:65:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0;i<l.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
n = 4, incorrect answer: jury 80 vs contestant 90 |
2 |
Halted |
0 ms |
0 KB |
- |