Submission #46902

# Submission time Handle Problem Language Result Execution time Memory
46902 2018-04-24T16:13:44 Z SpaimaCarpatilor Ancient Books (IOI17_books) C++17
22 / 100
2000 ms 32252 KB
#include "books.h"
#include<bits/stdc++.h>

using namespace std;

int N, S, p[1000009], prv[1000009], nxt[1000009], dp[1009][1009];
bool ap[1000009];
pair < int, int > jump[1009][1009];

void update (int &x, int y)
{
    if (y < x) x = y;
}

long long minimum_walk (vector < int > pp, int ss)
{
    N = pp.size (), S = ss + 1;
    for (int i=1; i<=N; i++)
        p[i] = pp[i - 1] + 1;
    for (int i=1; i<=N; i++)
        if (ap[i] == 0)
        {
            int j = p[i], maxRight = i;
            ap[i] = 1;
            while (j != i)
                ap[j] = 1, maxRight = max (maxRight, j), j = p[j];
            nxt[i] = maxRight, prv[i] = i, j = p[i];
            while (j != i)
                nxt[j] = maxRight, prv[j] = i, j = p[j];
        }
    long long ans = 0;
    for (int i=1; i<=N; i++)
    {
        int curr = i - p[i];
        if (curr < 0) curr = -curr;
        ans += curr;
    }
    for (int i=1; i<N; i++)
        if (nxt[i] > nxt[i + 1])
            nxt[i + 1] = nxt[i];
    int targetL = N + 1, targetR = 0;
    for (int i=1; i<=N; i++)
        if (p[i] != i)
        {
            if (targetL == N + 1)
                targetL = i;
            targetR = i;
        }
    if (targetR == 0) return ans;
    targetR = max (targetR, S);
    targetL = min (targetL, S);
    for (int i=1; i<=N; i++)
    {
        jump[i][i] = {prv[i], nxt[i]};
        for (int j=i + 1; j<=N; j++)
            jump[i][j].first = min (jump[i][j - 1].first, prv[j]),
            jump[i][j].second = max (jump[i][j - 1].second, nxt[j]);
    }
    for (int i=1; i<=N; i++)
        for (int j=N; j>=i; j--)
            if (jump[i][j].first != i || jump[i][j].second != j)
                jump[i][j] = jump[jump[i][j].first][jump[i][j].second];
    for (int i=1; i<=N; i++)
        for (int j=i; j<=N; j++)
            dp[i][j] = 3 * N + 10;
    dp[S][S] = 0;
    for (int i=S; i>=targetL; i--)
        for (int j=S; j<=targetR; j++)
        {
            if (jump[i][j].first != i || jump[i][j].second != j)
            {
                update (dp[jump[i][j].first][jump[i][j].second], dp[i][j]);
                continue;
            }
            if (j < N)
                update (dp[i][j + 1], dp[i][j] + 1);
            if (i > 1)
                update (dp[i - 1][j], dp[i][j] + 1);
        }
    return ans + 2LL * dp[targetL][targetR];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 0 ms 624 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 716 KB Output is correct
14 Correct 2 ms 716 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 2 ms 716 KB Output is correct
17 Correct 2 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 0 ms 624 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 716 KB Output is correct
14 Correct 2 ms 716 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 2 ms 716 KB Output is correct
17 Correct 2 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
19 Correct 11 ms 11520 KB Output is correct
20 Correct 11 ms 11520 KB Output is correct
21 Correct 2 ms 11520 KB Output is correct
22 Correct 11 ms 11520 KB Output is correct
23 Correct 11 ms 11520 KB Output is correct
24 Correct 12 ms 11600 KB Output is correct
25 Correct 12 ms 11600 KB Output is correct
26 Correct 12 ms 11600 KB Output is correct
27 Correct 12 ms 11600 KB Output is correct
28 Correct 12 ms 11600 KB Output is correct
29 Correct 12 ms 11600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 0 ms 624 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 716 KB Output is correct
14 Correct 2 ms 716 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 2 ms 716 KB Output is correct
17 Correct 2 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
19 Correct 11 ms 11520 KB Output is correct
20 Correct 11 ms 11520 KB Output is correct
21 Correct 2 ms 11520 KB Output is correct
22 Correct 11 ms 11520 KB Output is correct
23 Correct 11 ms 11520 KB Output is correct
24 Correct 12 ms 11600 KB Output is correct
25 Correct 12 ms 11600 KB Output is correct
26 Correct 12 ms 11600 KB Output is correct
27 Correct 12 ms 11600 KB Output is correct
28 Correct 12 ms 11600 KB Output is correct
29 Correct 12 ms 11600 KB Output is correct
30 Execution timed out 2070 ms 32252 KB Time limit exceeded
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 32252 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
5 Correct 2 ms 536 KB Output is correct
6 Correct 2 ms 624 KB Output is correct
7 Correct 2 ms 624 KB Output is correct
8 Correct 2 ms 624 KB Output is correct
9 Correct 2 ms 624 KB Output is correct
10 Correct 0 ms 624 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 716 KB Output is correct
14 Correct 2 ms 716 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 2 ms 716 KB Output is correct
17 Correct 2 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
19 Correct 11 ms 11520 KB Output is correct
20 Correct 11 ms 11520 KB Output is correct
21 Correct 2 ms 11520 KB Output is correct
22 Correct 11 ms 11520 KB Output is correct
23 Correct 11 ms 11520 KB Output is correct
24 Correct 12 ms 11600 KB Output is correct
25 Correct 12 ms 11600 KB Output is correct
26 Correct 12 ms 11600 KB Output is correct
27 Correct 12 ms 11600 KB Output is correct
28 Correct 12 ms 11600 KB Output is correct
29 Correct 12 ms 11600 KB Output is correct
30 Execution timed out 2070 ms 32252 KB Time limit exceeded
31 Halted 0 ms 0 KB -