# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
90706 | updown1 | Sailing Race (CEOI12_race) | C++17 | 2164 ms | 33792 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.
/*
dp[node 1][node 2][0/1] = max edges in the range starting at node 1/2 (based on the third dimension)
http://ceoi2012.elte.hu/download/modelsolutions/3_solrace.pdf
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, N)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e endl//"\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
const int MAXN=500, INF=1000000000;
///500,000,000
int N, K, dp[MAXN][MAXN][2], out, loc, dirb[MAXN][MAXN], dirs[MAXN][MAXN];
vector<int> adj[MAXN];
priority_queue<pair<int, int> > n1; /// ((-/+)relative loc, loc)
bool vis[MAXN];
bool in (int x, int y, int z) {
if (y < z) return y < x && x < z;
return x > y || x < z;
}
int solve(int n1, int n2, int at) {
if (dp[n1][n2][at] != -1) return dp[n1][n2][at];
dp[n1][n2][at] = 0;
//w<< n1 s n2 s at<<e;
/// range is n1+1 to n2-1 (n1+1)%N to (n2-1+N)%N
if ((n1+1)%N == n2) {dp[n1][n2][at] = 0; return dp[n1][n2][at];}
if (at == 0) {
/// at n1
for (int i: adj[n1]) if (in(i, n1, n2)) {
dp[n1][n2][0] = max(dp[n1][n2][0], max(solve(n1, i, 1), solve(i, n2, 0))+1);
}
return dp[n1][n2][0];
}
/// at n2
for (int i: adj[n2]) if (in(i, n1, n2)) {
dp[n1][n2][1] = max(dp[n1][n2][1], max(solve(i, n2, 0), solve(n1, i, 1))+1);
}
return dp[n1][n2][1];
}
main() {
//ifstream cin("test.in");
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
ffi {int a; cin >> a; while (a != 0) {adj[i].pb(a-1); cin >> a;}}
ffa dp[i][j][0] = dp[i][j][1] = -1;
ffa {solve(i, j, 0); solve(i, j, 1);}
ffa dirb[i][j] = dirs[i][j] = -INF;
if (K == 0) ffi{
if (dp[i][i][0] > out) {out = dp[i][i][0]; loc = i+1;}
}
else {
/// set dirb
ffi {
dirb[i][i] = 0;
n1.push(mp(0, i));
while (!n1.empty()) {
int x = n1.top().b; n1.pop();
for (int y: adj[x]) if (in(y, x, i)) {
/// moving forward
dirb[i][y] = max(dirb[i][y], dirb[i][x]+1);
n1.push(mp( -((y-i+N)%N) , y));
}
}
}
//ffa w<< i+1 s j+1 c dirb[i][j]<<e;
/// set dirs
ffi {
dirs[i][i] = 0;
n1.push(mp(0, i));
while (!n1.empty()) {
int x = n1.top().b; n1.pop();
for (int y: adj[x]) if (in(y, i, x)) {
/// moving forward
dirs[i][y] = max(dirs[i][y], dirs[i][x]+1);
n1.push(mp( (y-i+N)%N , y));
}
}
}
//ffa w<< i+1 s j+1 c dirs[i][j]<<e;
For (a, 0, N) for (int b: adj[a]) For (cc, 0, N) for (int d: adj[cc]) {
if (a == b || a == cc || a == d || b == cc || b == d || cc == d) continue;
if (in(cc, b, a) == in(d, b, a)) continue;
if (in(cc, b, a)) {
if (2+dirb[b][cc]+max(dp[d][b][0], dp[a][d][1]) > out) {
out = 2+dirb[b][cc]+max(dp[d][b][0], dp[a][d][1]);
loc = a+1;
}
}
else {
if (2+dirs[b][cc]+max(dp[d][a][0], dp[b][d][1]) > out) {
out = max(out, 2+dirs[b][cc]+max(dp[d][a][0], dp[b][d][1]));
loc = a+1;
}
}
/*if (out == 6) {
w<< in(cc, b, a)<<e;
w << a+1 s b+1 s cc+1 s d+1 c dirs[b][cc] s max(dp[d][a][0], dp[b][d][1])<<e;
return 0;
}*/
}
}
//ffa w<< i+1 s j+1 c solve(i, j, 0) s solve(i, j, 1)<<e;
w<< out <<e<< loc<<e;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |