# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
986346 | vjudge1 | Sailing Race (CEOI12_race) | C++17 | 363 ms | 5208 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int dp1[1000][1000],dp2[1000][1000];
pair<int,int>ans;
vector<int>adj[501],rev[501];
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,t;
cin>>n>>t;
for(int i=1;i<=n;i++){
int x;
cin>>x;
while(x){
adj[i].push_back(x);
rev[x].push_back(i);
cin>>x;
}
}
for(int d=1;d<=n;d++){
for(int i=1,j;i+d<=2*n;i++){
j=i+d;
for(auto x:adj[(i-1)%n+1]) {
if(i<x+n&&x+n<=j)x+=n; if(x<=i||j<x)continue;
dp1[i][j]=max(dp1[i][j],1+max(dp1[x][j],dp2[i][x]));
}
for(auto x:adj[(j-1)%n+1]) {
if(i<=x+n&&x+n<j)x+=n; if(x<i||j<=x)continue;
dp1[i][j]=max(dp1[i][j],1+max(dp1[x][j],dp2[i][x]));
}
}
}
for(int i=1;i<n;i++)
ans=max(ans,{dp1[i][i+n],i});
if(t==0||ans.first>n-2){
cout<<ans.first<<'\n'<<ans.second<<'\n';
return 0;
}
for(int B=1;B<=n;B++){
if(rev[B].empty())continue;
vector<int>dis(n+1);
for(auto i:adj[B])dis[i]=1;
for(int C=B%n+1;C-B;C=C%n+1){
if(!dis[C]) continue;
for(auto j:adj[C])
dis[j]=max(dis[j],dis[C]+1);
auto x=upper_bound(rev[B].begin(),rev[B].end(),C);
if(x==rev[B].end()) {
x=rev[B].begin();
if(*x>B)continue;
}
if(*x>B&&C<B)break;
for(auto D:adj[C]) if(*x>B){if(*x<D&&D<B)
ans=max(ans,{dis[C]+1+max(dp1[D][B],dp2[*x][D]),*x});
} else if(B<D||D<*x)
ans=max(ans,{dis[C]+1+max(dp1[D+n][*x+n],dp2[B][D+n]),*x});
}
}
for(int B=1;B<=n;B++){
if(rev[B].empty())continue;
vector<int>dis(n+1);
for(auto i:adj[B])dis[i]=1;
for(int C=(B+n-2)%n+1;C-B;C=(C+n-2)%n+1){
if(!dis[C]) continue;
for(auto j:adj[C])
dis[j]=max(dis[j],dis[C]+1);
auto x=lower_bound(rev[B].begin(),rev[B].end(),C);
if(x==rev[B].begin()) {
x=--rev[B].end();
if(*x<B)continue;
} else x--;
if(*x<B&&C>B)break;
for(auto D:adj[C]) if(*x>B){if(*x>D&&D>B)
ans=max(ans,{dis[C]+1+max(dp1[D][*x],dp2[B][D]),*x});
} else if(D>B||D<*x)
ans=max(ans,{dis[C]+1+max(dp1[D+n][B+n],dp2[*x][D+n]),*x});
}
}
cout<<ans.first<<'\n'<<ans.second<<'\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |