Submission #294225

# Submission time Handle Problem Language Result Execution time Memory
294225 2020-09-08T17:26:56 Z Kastanda Ancient Books (IOI17_books) C++11
100 / 100
283 ms 171512 KB
// M
#include<bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
const int N = 1000006, LG = 20;
int n, st, A[N], mnq[LG][N], mxq[LG][N], Log[N];
map < pair < int , int > , pair < int , int > > MP;
inline int GetMin(int l, int r)
{
        int lg = Log[r - l];
        return min(mnq[lg][l], mnq[lg][r - (1 << lg)]);
}
inline int GetMax(int l, int r)
{
        int lg = Log[r - l];
        return max(mxq[lg][l], mxq[lg][r - (1 << lg)]);
}
inline pair < int , int > Extend(pair < int , int > nw)
{
        while (true)
        {
                pair < int , int > tb;
                tb.first = min(nw.first, GetMin(nw.first, nw.second + 1));
                tb.second = max(nw.second, GetMax(nw.first, nw.second + 1));
                if (tb == nw)
                        break;
                nw = tb;
        }
        return nw;
}

long long minimum_walk(vector < int > _A, int _st)
{
        n = (int)_A.size();
        st = _st + 1;
        for (int i = 1; i <= n; i ++)
                A[i] = _A[i - 1] + 1;

        for (int i = 2; i <= n; i ++)
                Log[i] = Log[i >> 1] + 1;
        for (int i = 1; i <= n; i ++)
                mnq[0][i] = mxq[0][i] = A[i];
        for (int j = 1; j < LG; j ++)
                for (int i = 1; i + (1 << j) <= n + 1; i ++)
                {
                        mnq[j][i] = min(mnq[j - 1][i], mnq[j - 1][i + (1 << (j - 1))]);
                        mxq[j][i] = max(mxq[j - 1][i], mxq[j - 1][i + (1 << (j - 1))]);
                }

        ll SM = 0;
        for (int i = 1; i <= n; i ++)
                SM += abs(A[i] - i);

        int Mn = 1, Mx = n;
        while (Mn < st && A[Mn] == Mn)
                Mn ++;
        while (Mx > st && A[Mx] == Mx)
                Mx --;

        pair < int , int > nw = {st, st};
        nw = Extend(nw);

        while (nw != make_pair(Mn, Mx))
        {
                int cle = 0, cri = 0;
                pair < int , int > gle = nw, gri = nw;
                while (gle.first > Mn && gle.second == nw.second)
                        cle ++, gle = Extend({gle.first - 1, gle.second});
                while (gri.second < Mx && gri.first == nw.first)
                        cri ++, gri = Extend({gri.first, gri.second + 1});
                if (gle == gri)
                        SM += min(cle, cri) * 2, nw = gle;
                else
                        SM += (cle + cri) * 2, nw = make_pair(Mn, Mx);
        }
        return SM;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 278 ms 171164 KB Output is correct
31 Correct 282 ms 171128 KB Output is correct
32 Correct 260 ms 171128 KB Output is correct
33 Correct 278 ms 171256 KB Output is correct
34 Correct 271 ms 171256 KB Output is correct
35 Correct 275 ms 171384 KB Output is correct
36 Correct 269 ms 171384 KB Output is correct
37 Correct 266 ms 171388 KB Output is correct
38 Correct 267 ms 171204 KB Output is correct
39 Correct 266 ms 171128 KB Output is correct
40 Correct 278 ms 171256 KB Output is correct
41 Correct 281 ms 171252 KB Output is correct
42 Correct 281 ms 171256 KB Output is correct
43 Correct 262 ms 171256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 632 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 640 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 640 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 278 ms 171164 KB Output is correct
31 Correct 282 ms 171128 KB Output is correct
32 Correct 260 ms 171128 KB Output is correct
33 Correct 278 ms 171256 KB Output is correct
34 Correct 271 ms 171256 KB Output is correct
35 Correct 275 ms 171384 KB Output is correct
36 Correct 269 ms 171384 KB Output is correct
37 Correct 266 ms 171388 KB Output is correct
38 Correct 267 ms 171204 KB Output is correct
39 Correct 266 ms 171128 KB Output is correct
40 Correct 278 ms 171256 KB Output is correct
41 Correct 281 ms 171252 KB Output is correct
42 Correct 281 ms 171256 KB Output is correct
43 Correct 262 ms 171256 KB Output is correct
44 Correct 1 ms 640 KB Output is correct
45 Correct 1 ms 640 KB Output is correct
46 Correct 1 ms 632 KB Output is correct
47 Correct 1 ms 512 KB Output is correct
48 Correct 1 ms 640 KB Output is correct
49 Correct 1 ms 640 KB Output is correct
50 Correct 1 ms 640 KB Output is correct
51 Correct 1 ms 640 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
54 Correct 1 ms 640 KB Output is correct
55 Correct 1 ms 640 KB Output is correct
56 Correct 1 ms 640 KB Output is correct
57 Correct 1 ms 640 KB Output is correct
58 Correct 1 ms 512 KB Output is correct
59 Correct 1 ms 640 KB Output is correct
60 Correct 1 ms 640 KB Output is correct
61 Correct 1 ms 512 KB Output is correct
62 Correct 1 ms 640 KB Output is correct
63 Correct 1 ms 512 KB Output is correct
64 Correct 259 ms 171212 KB Output is correct
65 Correct 265 ms 171200 KB Output is correct
66 Correct 261 ms 171128 KB Output is correct
67 Correct 266 ms 171256 KB Output is correct
68 Correct 25 ms 15096 KB Output is correct
69 Correct 25 ms 15096 KB Output is correct
70 Correct 24 ms 15104 KB Output is correct
71 Correct 24 ms 15104 KB Output is correct
72 Correct 24 ms 15104 KB Output is correct
73 Correct 25 ms 15224 KB Output is correct
74 Correct 283 ms 171272 KB Output is correct
75 Correct 270 ms 171256 KB Output is correct
76 Correct 263 ms 171256 KB Output is correct
77 Correct 266 ms 171336 KB Output is correct
78 Correct 264 ms 171256 KB Output is correct
79 Correct 261 ms 171160 KB Output is correct
80 Correct 257 ms 171172 KB Output is correct
81 Correct 273 ms 171512 KB Output is correct
82 Correct 267 ms 171128 KB Output is correct
83 Correct 273 ms 171256 KB Output is correct
84 Correct 267 ms 171384 KB Output is correct
85 Correct 259 ms 171256 KB Output is correct
86 Correct 265 ms 171256 KB Output is correct
87 Correct 276 ms 171128 KB Output is correct
88 Correct 277 ms 171208 KB Output is correct
89 Correct 271 ms 171204 KB Output is correct
90 Correct 272 ms 171128 KB Output is correct
91 Correct 264 ms 171256 KB Output is correct
92 Correct 268 ms 171128 KB Output is correct