Submission #292955

# Submission time Handle Problem Language Result Execution time Memory
292955 2020-09-07T14:59:26 Z Kastanda Ancient Books (IOI17_books) C++11
50 / 100
565 ms 73068 KB
// M
#include<bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
const int N = 1000006;
int n, st, P[N], A[N], mn[N], mx[N];
int Find(int v)
{
        return (P[v] < 0 ? v : (P[v] = Find(P[v])));
}
inline void Merge(int v, int u)
{
        v = Find(v);
        u = Find(u);
        if (v == u)
                return ;
        P[u] = v;
        mn[v] = min(mn[v], mn[u]);
        mx[v] = max(mx[v], mx[u]);
}

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;

        ll SM = 0, tot = 0;
        for (int i = 1; i <= n; i ++)
                SM += abs(A[i] - i);
        memset(P, -1, sizeof(P));
        for (int i = 1; i <= n; i ++)
                mn[i] = mx[i] = i;
        set < int > ST;
        for (int i = 1; i <= n; i ++)
                ST.insert(i);

        for (int i = 1; i <= n; i ++)
                if (i != A[i])
                {
                        int l = min(i, A[i]);
                        int r = max(i, A[i]);
                        auto it = ST.lower_bound(l);
                        while (it != ST.end() && * it < r)
                                Merge(* it, (* it) + 1), it = ST.erase(it);
                }
        vector < pair < int , int > > vec;
        for (int i = 1; i <= n; i ++)
                if (Find(i) == i && i != A[i])
                        vec.push_back({mn[i], mx[i]});
        sort(vec.begin(), vec.end());
        while (vec.size() > 1 && vec[(int)vec.size() - 2].second >= st)
                tot += vec.back().first - vec[(int)vec.size() - 2].second, vec.pop_back();
        if (vec.size() && vec.back().first >= st)
                tot += vec.back().first - st, vec.pop_back();
        reverse(vec.begin(), vec.end());
        while (vec.size() > 1 && vec[(int)vec.size() - 2].first <= st)
                tot += vec[(int)vec.size() - 2].first - vec.back().second, vec.pop_back();
        if (vec.size() && vec.back().second <= st)
                tot += st - vec.back().second, vec.pop_back();
        if (vec.size())
        {
                assert(vec.size() == 1 && vec[0].first <= st && st <= vec[0].second);
                if (A[st] == st)
                {
                        int l = st;
                        while (l && A[l] == l)
                                l --;
                        assert(l >= vec[0].first);
                        int Best = st - l;
                        int r = st;
                        while (r <= n && A[r] == r)
                                r ++;
                        assert(r <= vec[0].second);
                        Best = min(Best, r - st);
                        tot += Best;
                }
        }

        tot = tot * 2 + SM;
        return tot;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4224 KB Output is correct
2 Correct 3 ms 4224 KB Output is correct
3 Correct 3 ms 4224 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4224 KB Output is correct
6 Correct 3 ms 4224 KB Output is correct
7 Correct 3 ms 4224 KB Output is correct
8 Correct 3 ms 4224 KB Output is correct
9 Correct 4 ms 4224 KB Output is correct
10 Correct 3 ms 4224 KB Output is correct
11 Correct 3 ms 4224 KB Output is correct
12 Correct 4 ms 4224 KB Output is correct
13 Correct 3 ms 4224 KB Output is correct
14 Correct 3 ms 4224 KB Output is correct
15 Correct 3 ms 4224 KB Output is correct
16 Correct 3 ms 4224 KB Output is correct
17 Correct 3 ms 4224 KB Output is correct
18 Correct 3 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4224 KB Output is correct
2 Correct 3 ms 4224 KB Output is correct
3 Correct 3 ms 4224 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4224 KB Output is correct
6 Correct 3 ms 4224 KB Output is correct
7 Correct 3 ms 4224 KB Output is correct
8 Correct 3 ms 4224 KB Output is correct
9 Correct 4 ms 4224 KB Output is correct
10 Correct 3 ms 4224 KB Output is correct
11 Correct 3 ms 4224 KB Output is correct
12 Correct 4 ms 4224 KB Output is correct
13 Correct 3 ms 4224 KB Output is correct
14 Correct 3 ms 4224 KB Output is correct
15 Correct 3 ms 4224 KB Output is correct
16 Correct 3 ms 4224 KB Output is correct
17 Correct 3 ms 4224 KB Output is correct
18 Correct 3 ms 4224 KB Output is correct
19 Correct 4 ms 4352 KB Output is correct
20 Correct 3 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 3 ms 4352 KB Output is correct
23 Correct 3 ms 4352 KB Output is correct
24 Correct 3 ms 4352 KB Output is correct
25 Correct 3 ms 4352 KB Output is correct
26 Correct 3 ms 4352 KB Output is correct
27 Correct 3 ms 4352 KB Output is correct
28 Correct 3 ms 4352 KB Output is correct
29 Correct 3 ms 4352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4224 KB Output is correct
2 Correct 3 ms 4224 KB Output is correct
3 Correct 3 ms 4224 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4224 KB Output is correct
6 Correct 3 ms 4224 KB Output is correct
7 Correct 3 ms 4224 KB Output is correct
8 Correct 3 ms 4224 KB Output is correct
9 Correct 4 ms 4224 KB Output is correct
10 Correct 3 ms 4224 KB Output is correct
11 Correct 3 ms 4224 KB Output is correct
12 Correct 4 ms 4224 KB Output is correct
13 Correct 3 ms 4224 KB Output is correct
14 Correct 3 ms 4224 KB Output is correct
15 Correct 3 ms 4224 KB Output is correct
16 Correct 3 ms 4224 KB Output is correct
17 Correct 3 ms 4224 KB Output is correct
18 Correct 3 ms 4224 KB Output is correct
19 Correct 4 ms 4352 KB Output is correct
20 Correct 3 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 3 ms 4352 KB Output is correct
23 Correct 3 ms 4352 KB Output is correct
24 Correct 3 ms 4352 KB Output is correct
25 Correct 3 ms 4352 KB Output is correct
26 Correct 3 ms 4352 KB Output is correct
27 Correct 3 ms 4352 KB Output is correct
28 Correct 3 ms 4352 KB Output is correct
29 Correct 3 ms 4352 KB Output is correct
30 Correct 470 ms 70848 KB Output is correct
31 Correct 473 ms 70776 KB Output is correct
32 Correct 424 ms 70856 KB Output is correct
33 Correct 527 ms 73068 KB Output is correct
34 Correct 537 ms 73068 KB Output is correct
35 Correct 565 ms 72880 KB Output is correct
36 Correct 542 ms 72048 KB Output is correct
37 Correct 501 ms 70980 KB Output is correct
38 Correct 506 ms 71032 KB Output is correct
39 Correct 509 ms 70972 KB Output is correct
40 Correct 497 ms 70776 KB Output is correct
41 Correct 522 ms 70720 KB Output is correct
42 Correct 502 ms 70896 KB Output is correct
43 Correct 449 ms 70776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4352 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 3 ms 4224 KB Output is correct
2 Correct 3 ms 4224 KB Output is correct
3 Correct 3 ms 4224 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4224 KB Output is correct
6 Correct 3 ms 4224 KB Output is correct
7 Correct 3 ms 4224 KB Output is correct
8 Correct 3 ms 4224 KB Output is correct
9 Correct 4 ms 4224 KB Output is correct
10 Correct 3 ms 4224 KB Output is correct
11 Correct 3 ms 4224 KB Output is correct
12 Correct 4 ms 4224 KB Output is correct
13 Correct 3 ms 4224 KB Output is correct
14 Correct 3 ms 4224 KB Output is correct
15 Correct 3 ms 4224 KB Output is correct
16 Correct 3 ms 4224 KB Output is correct
17 Correct 3 ms 4224 KB Output is correct
18 Correct 3 ms 4224 KB Output is correct
19 Correct 4 ms 4352 KB Output is correct
20 Correct 3 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 3 ms 4352 KB Output is correct
23 Correct 3 ms 4352 KB Output is correct
24 Correct 3 ms 4352 KB Output is correct
25 Correct 3 ms 4352 KB Output is correct
26 Correct 3 ms 4352 KB Output is correct
27 Correct 3 ms 4352 KB Output is correct
28 Correct 3 ms 4352 KB Output is correct
29 Correct 3 ms 4352 KB Output is correct
30 Correct 470 ms 70848 KB Output is correct
31 Correct 473 ms 70776 KB Output is correct
32 Correct 424 ms 70856 KB Output is correct
33 Correct 527 ms 73068 KB Output is correct
34 Correct 537 ms 73068 KB Output is correct
35 Correct 565 ms 72880 KB Output is correct
36 Correct 542 ms 72048 KB Output is correct
37 Correct 501 ms 70980 KB Output is correct
38 Correct 506 ms 71032 KB Output is correct
39 Correct 509 ms 70972 KB Output is correct
40 Correct 497 ms 70776 KB Output is correct
41 Correct 522 ms 70720 KB Output is correct
42 Correct 502 ms 70896 KB Output is correct
43 Correct 449 ms 70776 KB Output is correct
44 Incorrect 3 ms 4352 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -