Submission #982540

# Submission time Handle Problem Language Result Execution time Memory
982540 2024-05-14T11:58:33 Z PanosPask Sailing Race (CEOI12_race) C++14
35 / 100
3000 ms 45660 KB
#include <bits/stdc++.h>
#define pb push_back
#define MOD(var, M) (((var) >= (M)) ? ((var) - M) : (var))

using namespace std;

int N, K;
vector<vector<int>> adj_list;

// dp[l][r][k]:  Maximum number of stages if enclosed by l, r
// and being in the stages such that l < s < r'
// Where r' = l > r ? r = r + N : r

// k == 0: Starting in l
// k == 1: Starting in r
vector<vector<vector<int>>> dp;

// first[l][r][k]: True iff the first path in dp[l][r][k] is (l, r) or (r, l) respectively
vector<vector<vector<bool>>> first;

// Before the intersection with the first stage
// You can only choose one way to follow
// Either from l to r or from r to l
vector<vector<vector<int>>> bef;

// True if a stage can be organized from i to j
bool stage(int i, int j)
{
    int pos = lower_bound(adj_list[i].begin(), adj_list[i].end(), j) - adj_list[i].begin();
    if (pos < adj_list[i].size() && adj_list[i][pos] == j) {
        return true;
    }

    return false;
}

int main(void)
{
    scanf("%d %d", &N, &K);

    adj_list.resize(N);
    dp.assign(N, vector<vector<int>>(N, vector<int>(2, 0)));
    first.assign(N, vector<vector<bool>>(N, vector<bool>(2, false)));
    bef.assign(N, vector<vector<int>>(N, vector<int>(2, 0)));

    for (int i = 0; i < N; i++) {
        int v;
        scanf("%d", &v);

        while (v != 0) {
            v--;
            adj_list[i].pb(v);
            scanf("%d", &v);
        }

        sort(adj_list[i].begin(), adj_list[i].end());
    }

    int ans = 0;
    int starting = 0;

    for (int len = 1; len <= N; len++) {
        for (int l = 0; l < N; l++) {
            int r_actual = l + len - 1;
            int r = MOD(r_actual, N);


            for (int i_actual = l + 1; i_actual < r_actual; i_actual++) {
                int i = MOD(i_actual, N);

                dp[l][r][0] = max(dp[l][r][0], dp[l][i][0]);
                dp[l][r][1] = max(dp[l][r][1], dp[i][r][1]);



                if (stage(l, i)) {
                    dp[l][r][0] = max(dp[l][r][0], 1 + dp[i][r][0]);
                }
                if (stage(r, i)) {
                    dp[l][r][1] = max(dp[l][r][1], 1 + dp[l][i][1]);
                }
            }

            if (stage(l, r)) {
                int res = 1 + dp[MOD(l + 1, N)][r][1];
                if (res >= dp[l][r][0]) {
                    first[l][r][0] = true;
                    dp[l][r][0] = res;
                }
            }
            if (stage(r, l)) {
                int res = 1 + dp[l][MOD(r_actual - 1, N)][0];
                if (res >= dp[l][r][1]) {
                    first[l][r][1] = true;
                    dp[l][r][1] = res;
                }
            }

            if (dp[l][r][0] > ans) {
                ans = dp[l][r][0];
                starting = l;
            }
            if (dp[l][r][1] > ans) {
                ans = dp[l][r][1];
                starting = r;
            }
        }
    }

    printf("%d\n%d\n", ans, starting + 1);

    return 0;
}

Compilation message

race.cpp: In function 'bool stage(int, int)':
race.cpp:30:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     if (pos < adj_list[i].size() && adj_list[i][pos] == j) {
      |         ~~~~^~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int main()':
race.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     scanf("%d %d", &N, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~
race.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%d", &v);
      |         ~~~~~^~~~~~~~~~
race.cpp:53:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |             scanf("%d", &v);
      |             ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Incorrect 2 ms 604 KB Output isn't correct
5 Correct 4 ms 860 KB Output is correct
6 Incorrect 6 ms 860 KB Output isn't correct
7 Correct 11 ms 1344 KB Output is correct
8 Incorrect 10 ms 1372 KB Output isn't correct
9 Correct 23 ms 1884 KB Output is correct
10 Correct 16 ms 2304 KB Output is correct
11 Correct 32 ms 2140 KB Output is correct
12 Incorrect 173 ms 7760 KB Output isn't correct
13 Incorrect 448 ms 16476 KB Output isn't correct
14 Correct 965 ms 29336 KB Output is correct
15 Runtime error 2755 ms 45640 KB Memory limit exceeded
16 Execution timed out 3055 ms 45660 KB Time limit exceeded
17 Runtime error 2735 ms 45640 KB Memory limit exceeded
18 Runtime error 1622 ms 45564 KB Memory limit exceeded
19 Execution timed out 3063 ms 45660 KB Time limit exceeded
20 Execution timed out 3053 ms 45660 KB Time limit exceeded