답안 #293005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293005 2020-09-07T15:31:52 Z Kastanda 고대 책들 (IOI17_books) C++11
50 / 100
724 ms 85388 KB
// M
#include<bits/stdc++.h>
#include "books.h"
using namespace std;
typedef long long ll;
const int N = 1000006;
int n, st, k, T[N], P[N], A[N], mn[N], mx[N];
vector < tuple < int , int , int > > E;
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]);
}
inline void AddEdge(int v, int u, int w)
{
        E.push_back({w, v, 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);

        vector < int > M(n + 1, 0);
        vector < pair < int , int > > Roller;
        for (int i = 1; i <= n; i ++)
                if (!M[i] && A[i] != i)
                {
                        auto Do = [&](int l, int r){
                                if (l >= r)
                                        return ;
                                auto it = ST.lower_bound(l);
                                while (it != ST.end() && * it < r)
                                        Merge(* it, (* it) + 1), it = ST.erase(it);
                                return ;
                        };

                        vector < int > V;
                        int nw = i;
                        while (!M[nw])
                                V.push_back(nw), M[nw] = 1, nw = A[nw];
                        sort(V.begin(), V.end());
                        int is = 0;
                        for (int j : V)
                                if (j == st)
                                        is = 1;
                        for (int j = 0; j + 1 < (int)V.size(); j ++)
                        {
                                if (V[j + 1] < st || V[j] > st || is)
                                        Do(V[j], V[j + 1]);
                                else
                                        Roller.push_back({V[j], V[j + 1]});
                        }
                }

        /*
        for (int i = 1; i <= n; i ++)
                if (i != A[i])
                {
                                        int l = min(i, A[i]);
                        int r = max(i, A[i]);

                        Do(l, min(r, st - 1));
                        Do(max(st + 1, l), r);
                }
        */
        vector < pair < pair < int , int > , int > > vec;
        for (int i = 1; i <= n; i ++)
                if ((Find(i) == i && i != A[i]))
                        T[i] = ++ k, vec.push_back({{mn[i], mx[i]}, T[i]});
        if (Find(st) == st && A[st] == st)
        {
                T[st] = ++ k;
                vec.push_back({{st, st}, T[st]});
        }
        sort(vec.begin(), vec.end());


        //for (auto X : vec)
        //      printf("%d , %d :: %d\n", X.first.first, X.first.second, X.second);

        //assert(Find(st) == st);
        /*for (int i = 1; i <= n; i ++)
                if (i != A[i])
                {
                        int l = min(i, A[i]);
                        int r = max(i, A[i]);

                        if (l <= st && st <= r)
                                AddEdge(T[Find(l)], T[Find(r)], 0);
                }*/

        for (auto X : Roller)
        {
                int v = Find(X.first);
                int u = Find(X.second);
                AddEdge(T[v], T[u], 0);
        }

        for (int i = 0; i + 1 < k; i ++)
                AddEdge(vec[i].second, vec[i + 1].second, vec[i + 1].first.first - vec[i].first.second);

        sort(E.begin(), E.end());
        memset(P, -1, sizeof(P));
        int cc = 0;
        for (auto X : E)
        {
                int w, v, u;
                tie(w, v, u) = X;
                v = Find(v);
                u = Find(u);
                if (v == u)
                        continue;
                tot += w * 2;
                Merge(v, u);
                cc ++;
        }
        assert(cc == k - 1);

        tot = tot + SM;
        return tot;

        /*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;*/
}
# 결과 실행 시간 메모리 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 4 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 4 ms 4224 KB Output is correct
12 Correct 3 ms 4224 KB Output is correct
13 Correct 4 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
# 결과 실행 시간 메모리 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 4 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 4 ms 4224 KB Output is correct
12 Correct 3 ms 4224 KB Output is correct
13 Correct 4 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 5 ms 4352 KB Output is correct
20 Correct 4 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 4 ms 4352 KB Output is correct
23 Correct 4 ms 4352 KB Output is correct
24 Correct 4 ms 4352 KB Output is correct
25 Correct 4 ms 4352 KB Output is correct
26 Correct 4 ms 4352 KB Output is correct
27 Correct 4 ms 4352 KB Output is correct
28 Correct 4 ms 4352 KB Output is correct
29 Correct 4 ms 4352 KB Output is correct
# 결과 실행 시간 메모리 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 4 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 4 ms 4224 KB Output is correct
12 Correct 3 ms 4224 KB Output is correct
13 Correct 4 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 5 ms 4352 KB Output is correct
20 Correct 4 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 4 ms 4352 KB Output is correct
23 Correct 4 ms 4352 KB Output is correct
24 Correct 4 ms 4352 KB Output is correct
25 Correct 4 ms 4352 KB Output is correct
26 Correct 4 ms 4352 KB Output is correct
27 Correct 4 ms 4352 KB Output is correct
28 Correct 4 ms 4352 KB Output is correct
29 Correct 4 ms 4352 KB Output is correct
30 Correct 724 ms 78900 KB Output is correct
31 Correct 680 ms 76932 KB Output is correct
32 Correct 425 ms 74744 KB Output is correct
33 Correct 643 ms 85356 KB Output is correct
34 Correct 615 ms 85356 KB Output is correct
35 Correct 701 ms 85388 KB Output is correct
36 Correct 629 ms 81972 KB Output is correct
37 Correct 570 ms 78792 KB Output is correct
38 Correct 624 ms 77124 KB Output is correct
39 Correct 589 ms 76876 KB Output is correct
40 Correct 644 ms 75076 KB Output is correct
41 Correct 684 ms 75888 KB Output is correct
42 Correct 686 ms 75876 KB Output is correct
43 Correct 539 ms 74696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 4352 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4030'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 4 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 4 ms 4224 KB Output is correct
12 Correct 3 ms 4224 KB Output is correct
13 Correct 4 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 5 ms 4352 KB Output is correct
20 Correct 4 ms 4352 KB Output is correct
21 Correct 4 ms 4352 KB Output is correct
22 Correct 4 ms 4352 KB Output is correct
23 Correct 4 ms 4352 KB Output is correct
24 Correct 4 ms 4352 KB Output is correct
25 Correct 4 ms 4352 KB Output is correct
26 Correct 4 ms 4352 KB Output is correct
27 Correct 4 ms 4352 KB Output is correct
28 Correct 4 ms 4352 KB Output is correct
29 Correct 4 ms 4352 KB Output is correct
30 Correct 724 ms 78900 KB Output is correct
31 Correct 680 ms 76932 KB Output is correct
32 Correct 425 ms 74744 KB Output is correct
33 Correct 643 ms 85356 KB Output is correct
34 Correct 615 ms 85356 KB Output is correct
35 Correct 701 ms 85388 KB Output is correct
36 Correct 629 ms 81972 KB Output is correct
37 Correct 570 ms 78792 KB Output is correct
38 Correct 624 ms 77124 KB Output is correct
39 Correct 589 ms 76876 KB Output is correct
40 Correct 644 ms 75076 KB Output is correct
41 Correct 684 ms 75888 KB Output is correct
42 Correct 686 ms 75876 KB Output is correct
43 Correct 539 ms 74696 KB Output is correct
44 Incorrect 4 ms 4352 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4030'
45 Halted 0 ms 0 KB -