답안 #765970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765970 2023-06-25T08:20:35 Z boris_mihov 고대 책들 (IOI17_books) C++17
50 / 100
98 ms 24692 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--;
            }

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

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

        if (myL == neededL && myR == neededR)
        {
            break;
        }

        int addL = (myL > neededL) * 2;
        int addR = (myR < neededR) * 2;
        int nextL = myL - 1;
        int nextR = myR + 1;
        int ptrNextL = myL - 1; 
        int ptrNextR = myR + 1;
        int min, max; 

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

            if (max >= s)
            {
                break;
            }

            if (ptrNextL > neededL)
            {
                addL += 2;
                ptrNextL--;
            }
        }

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

            if (min <= s)
            {
                break;
            }

            if (ptrNextR < neededR)
            {
                addR += 2;
                ptrNextR++;
            }
        }

        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;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 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 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 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 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 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 0 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 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 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 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 0 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 0 ms 340 KB Output is correct
30 Correct 97 ms 16948 KB Output is correct
31 Correct 98 ms 16900 KB Output is correct
32 Correct 82 ms 24692 KB Output is correct
33 Correct 88 ms 22256 KB Output is correct
34 Correct 92 ms 22348 KB Output is correct
35 Correct 91 ms 21236 KB Output is correct
36 Correct 85 ms 18928 KB Output is correct
37 Correct 81 ms 17152 KB Output is correct
38 Correct 80 ms 16884 KB Output is correct
39 Correct 80 ms 16976 KB Output is correct
40 Correct 83 ms 16884 KB Output is correct
41 Correct 92 ms 16884 KB Output is correct
42 Correct 90 ms 16884 KB Output is correct
43 Correct 85 ms 22644 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10474'
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 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 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 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 0 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 0 ms 340 KB Output is correct
30 Correct 97 ms 16948 KB Output is correct
31 Correct 98 ms 16900 KB Output is correct
32 Correct 82 ms 24692 KB Output is correct
33 Correct 88 ms 22256 KB Output is correct
34 Correct 92 ms 22348 KB Output is correct
35 Correct 91 ms 21236 KB Output is correct
36 Correct 85 ms 18928 KB Output is correct
37 Correct 81 ms 17152 KB Output is correct
38 Correct 80 ms 16884 KB Output is correct
39 Correct 80 ms 16976 KB Output is correct
40 Correct 83 ms 16884 KB Output is correct
41 Correct 92 ms 16884 KB Output is correct
42 Correct 90 ms 16884 KB Output is correct
43 Correct 85 ms 22644 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 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '10478', found: '10474'
52 Halted 0 ms 0 KB -