Submission #765982

# Submission time Handle Problem Language Result Execution time Memory
765982 2023-06-25T08:26:00 Z boris_mihov Ancient Books (IOI17_books) C++17
100 / 100
122 ms 31476 KB
#include "books.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

typedef long long llong;
const int MAXN = 1000000 + 10;
const int INF  = 1e9;

int n, s;
int p[MAXN];
int add[MAXN];
bool vis[MAXN];
int lCycle[MAXN];
int rCycle[MAXN];
int inCycle[MAXN];

llong minimum_walk(std::vector <int> P, int S) 
{
    llong ans = 0;
    n = P.size(); s = S + 1;
    for (int i = 1 ; i <= n ; ++i)
    {
        p[i] = P[i - 1] + 1;
        ans += abs(i - p[i]);
    }

    int max = 0;
    int cnt = 0;
    int neededL = s;
    int neededR = s;
    for (int i = 1 ; i <= n ; ++i)
    {
        if (vis[i])
        {
            continue;
        }

        cnt++;
        int curr = i;
        lCycle[cnt] = rCycle[cnt] = i;
        while (!vis[curr])
        {
            vis[curr] = true;
            inCycle[curr] = cnt;
            lCycle[cnt] = std::min(lCycle[cnt], curr);
            rCycle[cnt] = std::max(rCycle[cnt], curr);
            curr = p[curr];
        }

        if (curr != p[curr])
        {
            neededL = std::min(neededL, lCycle[cnt]);
            neededR = std::max(neededR, rCycle[cnt]);
        }
    }

    int myL = s;
    int myR = s;
    int ptrL = lCycle[inCycle[s]];
    int ptrR = rCycle[inCycle[s]];

    while (true)
    {
        while (true)
        {
            while (myL >= ptrL)
            {
                ptrL = std::min(ptrL, lCycle[inCycle[myL]]);
                ptrR = std::max(ptrR, rCycle[inCycle[myL]]);
                myL--;
            }

            while (myR <= ptrR)
            {
                ptrL = std::min(ptrL, lCycle[inCycle[myR]]);
                ptrR = std::max(ptrR, rCycle[inCycle[myR]]);
                myR++;
            }

            if (ptrL == myL + 1 && ptrR == myR - 1)
            {
                myL++;
                myR--;
                break;
            }
        }

        if (myL == neededL && myR == neededR)
        {
            break;
        }
        
        int addL = 0;
        int addR = 0;
        int nextL = myL - 1;
        int nextR = myR + 1;
        int ptrNextL = myL; 
        int ptrNextR = myR;
        int min, max; 

        max = 0;
        while (ptrNextL > neededL)
        {
            addL += 2;
            ptrNextL--;
            while (nextL >= ptrNextL)
            {
                max = std::max(max, rCycle[inCycle[nextL]]);
                ptrNextL = std::min(ptrNextL, lCycle[inCycle[nextL]]);
                nextL--;
            }

            if (max >= s)
            {
                break;
            }
        }

        min = n + 1;
        while (ptrNextR < neededR)
        {
            addR += 2;
            ptrNextR++;
            while (nextR <= ptrNextR)
            {
                min = std::min(min, lCycle[inCycle[nextR]]);
                ptrNextR = std::max(ptrNextR, rCycle[inCycle[nextR]]);
                nextR++;
            }

            if (min <= s)
            {
                break;
            }
        }

        if (max < s)
        {
            ans += addL;
            ans += addR;
            break;
        }

        ans += std::min(addL, addR);
        ptrL = ptrNextL;
        ptrR = ptrNextR;
    }

	return ans;
}

Compilation message

books.cpp: In function 'llong minimum_walk(std::vector<int>, int)':
books.cpp:29:9: warning: unused variable 'max' [-Wunused-variable]
   29 |     int max = 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 110 ms 16888 KB Output is correct
31 Correct 105 ms 16884 KB Output is correct
32 Correct 78 ms 24700 KB Output is correct
33 Correct 85 ms 22260 KB Output is correct
34 Correct 94 ms 22256 KB Output is correct
35 Correct 89 ms 21232 KB Output is correct
36 Correct 84 ms 18932 KB Output is correct
37 Correct 77 ms 17140 KB Output is correct
38 Correct 94 ms 16888 KB Output is correct
39 Correct 78 ms 17008 KB Output is correct
40 Correct 80 ms 16884 KB Output is correct
41 Correct 90 ms 16880 KB Output is correct
42 Correct 88 ms 16888 KB Output is correct
43 Correct 84 ms 22716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 312 KB Output is correct
20 Correct 0 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 110 ms 16888 KB Output is correct
31 Correct 105 ms 16884 KB Output is correct
32 Correct 78 ms 24700 KB Output is correct
33 Correct 85 ms 22260 KB Output is correct
34 Correct 94 ms 22256 KB Output is correct
35 Correct 89 ms 21232 KB Output is correct
36 Correct 84 ms 18932 KB Output is correct
37 Correct 77 ms 17140 KB Output is correct
38 Correct 94 ms 16888 KB Output is correct
39 Correct 78 ms 17008 KB Output is correct
40 Correct 80 ms 16884 KB Output is correct
41 Correct 90 ms 16880 KB Output is correct
42 Correct 88 ms 16888 KB Output is correct
43 Correct 84 ms 22716 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 1 ms 352 KB Output is correct
52 Correct 1 ms 316 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 312 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 0 ms 312 KB Output is correct
63 Correct 0 ms 312 KB Output is correct
64 Correct 87 ms 29304 KB Output is correct
65 Correct 89 ms 29996 KB Output is correct
66 Correct 82 ms 23916 KB Output is correct
67 Correct 83 ms 23792 KB Output is correct
68 Correct 9 ms 2772 KB Output is correct
69 Correct 9 ms 2696 KB Output is correct
70 Correct 15 ms 2772 KB Output is correct
71 Correct 10 ms 3092 KB Output is correct
72 Correct 9 ms 3140 KB Output is correct
73 Correct 9 ms 2496 KB Output is correct
74 Correct 96 ms 29040 KB Output is correct
75 Correct 100 ms 29044 KB Output is correct
76 Correct 91 ms 29428 KB Output is correct
77 Correct 93 ms 29424 KB Output is correct
78 Correct 87 ms 28544 KB Output is correct
79 Correct 91 ms 28644 KB Output is correct
80 Correct 96 ms 31476 KB Output is correct
81 Correct 90 ms 27496 KB Output is correct
82 Correct 91 ms 27512 KB Output is correct
83 Correct 98 ms 26580 KB Output is correct
84 Correct 91 ms 25976 KB Output is correct
85 Correct 89 ms 24688 KB Output is correct
86 Correct 87 ms 24080 KB Output is correct
87 Correct 93 ms 30284 KB Output is correct
88 Correct 122 ms 29216 KB Output is correct
89 Correct 96 ms 27264 KB Output is correct
90 Correct 84 ms 23960 KB Output is correct
91 Correct 94 ms 23672 KB Output is correct
92 Correct 85 ms 23688 KB Output is correct