Submission #127030

# Submission time Handle Problem Language Result Execution time Memory
127030 2019-07-08T20:11:42 Z eriksuenderhauf Sailing Race (CEOI12_race) C++11
75 / 100
3000 ms 6904 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e2 + 2;
const int INF = 1e9 + 7;
int ans[MAXN], mx[MAXN][MAXN][2];
int dp[MAXN][MAXN][2], dp2[MAXN][MAXN][2];
bool adj[MAXN][MAXN];
vector<int> nex[MAXN];
int n;

bool between(int l, int r, int x) {
    if (l > r)
        return l < x || x < r;
    return l < x && x < r;
}

int solve(int l, int r, int k) {// path ends at r, interval at l
    if (~dp[l][r][k]) return dp[l][r][k];
    int &ret = dp[l][r][k];
    ret = 0;
    int lo = k ? r : l;
    int hi = l ^ r ^ lo;
    for (int i: nex[r])
        if (between(lo, hi, i))
            ret = max(ret, max(solve(r, i, k^1),  solve(l, i, k)) + 1);
    return ret;
}

int f2(int l, int r, int k) {
    if (l == r) return 0;
    if (~dp2[l][r][k]) return dp2[l][r][k];
    int ret = adj[l][r] ? 0 : -INF;
    int lo = k ? r : l;
    int hi = l ^ r ^ lo;
    for (int i: nex[l])
        if (between(lo, hi, i))
            ret = max(ret, f2(i, r, k));
    return dp2[l][r][k] = ret + 1;
}

int main() {
    memset(dp, -1, sizeof dp);
    memset(dp2, -1, sizeof dp2);
    int k;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
        int x;
        while (1) {
            scanf("%d", &x);
            if (x == 0)
                break;
            adj[i][x] = 1;
            nex[i].push_back(x);
        }
    }
    for (int i = 1; i <= n; i++)
        for (int j: nex[i])
            ans[i] = max(ans[i], max(solve(i, j, 0), solve(i, j, 1)) + 1);
    if (k == 1) for (int i = 1; i <= n; i++) {
        memset(mx, 0, sizeof mx);
        for (int x = (i%n) + 1; x != i; x = (x%n) + 1)
            for (int y = (x+1)%n + 1; y != i; y = (y%n) + 1)
                if (adj[i][(y+n-2)%n + 1])
                    mx[x][y][0] = max(solve(x, (y+n-2)%n + 1, 0) + 1, mx[x][(y+n-2)%n + 1][0]);
                else
                    mx[x][y][0] = max(0, mx[x][(y+n-2)%n + 1][0]);
        for (int x = (i+n-2)%n+1; x != i; x = (x-2+n)%n+1)
            for (int y = (x+2*n-3)%n+1; y != i; y = (y-2+n)%n+1)
                if (adj[i][y%n+1])
                    mx[y][x][1] = max(solve(x, y%n+1, 1) + 1, mx[y%n+1][x][1]);
                else
                    mx[y][x][1] = max(0, mx[y%n+1][x][1]);

        for (int l = i%n+1; l != (i+2*n-3)%n+1; l = (l%n) + 1)
            for (int j = (l+1)%n+1; j != i; j = (j%n) + 1) {
                if (adj[l][j])
                    ans[l] = max(ans[l], max(f2(j, i, 0) + mx[l][j][0], f2(j, i, 0) + mx[l][j][1]) + 1);
                if (adj[j][l])
                    ans[j] = max(ans[j], max(f2(l, i, 1) + mx[l][j][0], f2(l, i, 1) + mx[l][j][1]) + 1);
            }
        if (ans[i] == n) break;
    }
    int ind = 0;
    for (int i = 1; i <= n; i++)
        if (ans[ind] < ans[i])
            ind = i;
    printf("%d\n%d\n", ans[ind], ind);
    return 0;
}

Compilation message

race.cpp: In function 'int main()':
race.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
race.cpp:50:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &x);
             ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 9 ms 6264 KB Output is correct
3 Correct 10 ms 6264 KB Output is correct
4 Correct 14 ms 6264 KB Output is correct
5 Correct 6 ms 4344 KB Output is correct
6 Correct 19 ms 6320 KB Output is correct
7 Correct 8 ms 4344 KB Output is correct
8 Correct 28 ms 6336 KB Output is correct
9 Correct 9 ms 4348 KB Output is correct
10 Correct 16 ms 4344 KB Output is correct
11 Correct 11 ms 4348 KB Output is correct
12 Correct 234 ms 6436 KB Output is correct
13 Correct 650 ms 6520 KB Output is correct
14 Correct 61 ms 4572 KB Output is correct
15 Execution timed out 3049 ms 6644 KB Time limit exceeded
16 Execution timed out 3047 ms 6776 KB Time limit exceeded
17 Execution timed out 3021 ms 6652 KB Time limit exceeded
18 Correct 77 ms 4600 KB Output is correct
19 Execution timed out 3044 ms 6752 KB Time limit exceeded
20 Execution timed out 3040 ms 6904 KB Time limit exceeded