# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
260145 | MiricaMatei | Sailing Race (CEOI12_race) | C++14 | 987 ms | 7672 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;
const int MAXN = 505;
int n;
vector<int>G[MAXN];
int dp1[MAXN][MAXN][3];
int dp2[MAXN][MAXN][3];
int mc[MAXN][MAXN];
int prv(int i) {
i--;
if (i == 0)
i = n;
return i;
}
int nxt(int i) {
i++;
if (i == n + 1)
i = 1;
return i;
}
int solve1(int x, int y, int t) {
if (dp1[x][y][t] != -1)
return dp1[x][y][t];
if (t == 0) {
int mx = 0;
for (int i = y; i != x; i = prv(i))
if (mc[y][i])
mx = max(mx, 1 + max(solve1(x, i, 0), solve1(y, i, 1)));
dp1[x][y][t] = mx;
} else {
int mx = 0;
for (int i = y; i != x; i = nxt(i))
if (mc[y][i])
mx = max(mx, 1 + max(solve1(y, i, 0), solve1(x, i, 1)));
dp1[x][y][t] = mx;
}
return dp1[x][y][t];
}
int main() {
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int p;
scanf("%d%d", &n, &p);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
while (x != 0) {
G[i].push_back(x);
mc[i][x] = 1;
scanf("%d", &x);
}
for (int j = 1; j <= n; ++j)
for (int t = 0; t <= 1; ++t)
dp1[i][j][t] = dp2[i][j][t] = -1;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
dp1[i][j][0] = solve1(i, j, 0);
dp1[i][j][1] = solve1(i, j, 1);
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
for (int t = 0; t <= 1; ++t) {
if (mc[i][j]) {
dp1[i][j][t] = max(dp1[i][j][t], 0);
dp1[i][j][t]++;
}
}
}
int ans = 0, w = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
for (int t = 0; t <= 1; ++t)
if (mc[i][j] && dp1[i][j][t] > ans) {
ans = dp1[i][j][t];
w = i;
}
if (p == 1) {
for (int i = 1; i <= n; ++i) {
for (int j = nxt(i); j != i; j = nxt(j)) {
if (mc[j][i])
dp2[j][i][0] = 1;
for (int k = prv(j); k != i; k = prv(k))
if (mc[j][k])
dp2[j][i][0] = max(dp2[j][i][0], dp2[k][i][0] + 1);
}
for (int j = prv(i); j != i; j = prv(j)) {
if (mc[j][i])
dp2[j][i][1] = 1;
for (int k = nxt(j); k != i; k = nxt(k))
if (mc[j][k])
dp2[j][i][1] = max(dp2[j][i][1], dp2[k][i][1] + 1);
}
}
/*for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (mc[i][j]) {
for (int k = prv(j); k != i; k = prv(k)) {
for (int l = nxt(j); l != i; l = nxt(l)) {
if (mc[k][l]) {
int sol = 1 + dp2[j][k][0] + 1 + max(dp1[j][l][0] - mc[j][l], dp1[i][l][1] - mc[i][l]);
if (sol > ans) {
ans = sol;
w = i;
}
}
}
}
for (int k = nxt(j); k != i; k = nxt(k)) {
for (int l = prv(j); l != i; l = prv(l)) {
if (mc[k][l]) {
int sol = 1 + dp2[j][k][1] + 1 + max(dp1[j][l][1] - mc[j][l], dp1[i][l][0] - mc[i][l]);
if (sol > ans) {
ans = sol;
w = i;
}
}
}
}
}
}
}*/
}
printf("%d\n%d\n", ans, w);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |