답안 #694040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
694040 2023-02-03T15:54:17 Z sharaelong 고대 책들 (IOI17_books) C++17
22 / 100
2000 ms 105768 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

struct DisjointSet {
    int n;
    vector<int> parent, rank;
    DisjointSet(int _n) : n(_n) {
        parent.resize(n);
        iota(parent.begin(), parent.end(), 0);
        rank.resize(n, 0);
    }

    int find(int u) {
        return parent[u] = (u == parent[u] ? u : find(parent[u]));
    }

    void merge(int u, int v) {
        u = find(u); v = find(v);
        if (u == v) return;
        if (rank[u] > rank[v]) swap(u, v);
        parent[u] = v;
        if (rank[u] == rank[v]) ++rank[v];
    }
};

struct FenwickTree {
    vector<int> tree;
    FenwickTree(int size) {
        tree.resize(size+1, 0);
    }

    int sum(int pos) {
        int ret = 0;
        for (int i=pos+1; i>0; i &= (i-1)) ret += tree[i];
        return ret;
    }

    void add(int pos, int val) {
        for (int i=pos+1; i<tree.size(); i+=(i & -i)) tree[i] += val;
    }
};

const int MAX_N = 1e6 + 1;
const ll INF = 4e18;

DisjointSet dsu(MAX_N);
FenwickTree fen(MAX_N);
bool visited[MAX_N];
vector<int> cycle[MAX_N];
vector<int> adj[MAX_N];

void dfs(int here, int parent = -1) {
    int dhere = dsu.find(here);
    // cout << here << ' ' << parent << ": ";
    // for (int x: cycle[dhere]) cout << x << ' ';
    // cout << endl;
    bool merged = false;
    if (parent != -1) {
        parent = dsu.find(parent);
        int mn = *min_element(cycle[dhere].begin(), cycle[dhere].end());
        int mx = *max_element(cycle[dhere].begin(), cycle[dhere].end());
        if (fen.sum(mx) - fen.sum(mn) > 0) {
            dsu.merge(here, parent);
            for (int x: cycle[dhere]) fen.add(x, 1);
            vector<int> tmp;
            for (int x: cycle[dhere]) tmp.push_back(x);
            for (int x: cycle[parent]) tmp.push_back(x);
            swap(tmp, cycle[dsu.find(parent)]);
            merged = true;
        }
    }
    
    if (!merged) {
        for (int x: cycle[dhere]) assert(fen.sum(x)-fen.sum(x-1) == 0);
        for (int x: cycle[dhere]) fen.add(x, 1);
    }
    for (int there: adj[here]) {
        dfs(there, here);
    }
    if (!merged) {
        for (int x: cycle[dhere]) fen.add(x, -1);
        for (int x: cycle[dhere]) assert(fen.sum(x)-fen.sum(x-1)==0);
    }
}

ll minimum_walk(vector<int> p, int s) {
    int n = p.size();
    vector<pair<pii, int>> range;
    ll jump_len = 0;
    for (int i=0; i<n; ++i) {
        if (!visited[i]) {
            int l = i, r = i;
            jump_len += abs(i-p[i]);
            visited[i] = true;
            int x = p[i];
            while (x != i) {
                l = min(l, x);
                r = max(r, x);
                jump_len += abs(x-p[x]);
                visited[x] = true;
                dsu.merge(x, i);
                x = p[x];
            }
            if (l < r) range.push_back({ { l, r }, i });
        }
    }
    
    sort(range.begin(), range.end());
    // for (int i=0; i<range.size(); ) {
    //     auto[l, r] = range[i].first;
    //     int j = i+1;
    //     while (j < range.size() && range[j].first.first < r) {
    //         dsu.merge(range[i].second, range[j].second);
    //         r = max(r, range[j].first.second);
    //         j++;
    //     }
    //     i = j;
    // }
    for (int i=0; i<range.size(); ++i) {
        for (int j=i+1; j<range.size(); ++j) {
            auto[l1, r1] = range[i].first;
            auto[l2, r2] = range[j].first;
            if (l2 < r1 && r1 < r2) dsu.merge(range[i].second, range[j].second);
        }
    }
    
    vector<pair<pii, int>> dfs_order;
    for (int i=0; i<n; ++i) cycle[dsu.find(i)].push_back(i);
    for (int i=0; i<n; ++i) {
        if (dsu.find(i) == i && cycle[i].size() >= 2) {
            dfs_order.push_back({ { cycle[i][0], cycle[i].back() }, i });
            // cout << cycle[i][0] << ' ' << cycle[i].back() << ' ' << i << endl;
        }
    }
    sort(dfs_order.begin(), dfs_order.end());
    
    vector<pii> st;
    vector<int> roots;
    for (int i=0; i<dfs_order.size(); ++i) {
        while (!st.empty() && st.back().first < dfs_order[i].first.first) st.pop_back();
        if (!st.empty()) adj[st.back().second].push_back(dfs_order[i].second);
        else roots.push_back(dfs_order[i].second);
        st.push_back({ dfs_order[i].first.second, dfs_order[i].second });
    }
    for (int r: roots) dfs(r);

    ll ans = 0;
    for (int i=0; i<roots.size(); ++i) {
        int r = dsu.find(roots[i]);
        sort(cycle[r].begin(), cycle[r].end());
        // cout << r << ": ";
        // for (int x: cycle[r]) cout << x << ' ';
        // cout << endl;
        if (i > 0) ans += cycle[r][0];
        if (i+1 < roots.size()) ans -= cycle[r].back();
        
        if (cycle[r][0] <= s && s <= cycle[r].back()) {
            ll tmp = INF;
            for (int x: cycle[r]) tmp = min(tmp, (ll)abs(x-s));
            ans += tmp;
        }
    }
    if (!roots.empty()) {
        if (s < cycle[dsu.find(roots[0])][0]) ans += cycle[dsu.find(roots[0])][0]-s;
        if (s > cycle[dsu.find(roots.back())].back()) ans += s-cycle[dsu.find(roots.back())].back();
    }
    return jump_len + 2ll * ans;
}

Compilation message

books.cpp: In member function 'void FenwickTree::add(int, int)':
books.cpp:42:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         for (int i=pos+1; i<tree.size(); i+=(i & -i)) tree[i] += val;
      |                           ~^~~~~~~~~~~~
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:122:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for (int i=0; i<range.size(); ++i) {
      |                   ~^~~~~~~~~~~~~
books.cpp:123:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (int j=i+1; j<range.size(); ++j) {
      |                         ~^~~~~~~~~~~~~
books.cpp:142:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |     for (int i=0; i<dfs_order.size(); ++i) {
      |                   ~^~~~~~~~~~~~~~~~~
books.cpp:151:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |     for (int i=0; i<roots.size(); ++i) {
      |                   ~^~~~~~~~~~~~~
books.cpp:158:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |         if (i+1 < roots.size()) ans -= cycle[r].back();
      |             ~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 58964 KB Output is correct
2 Correct 27 ms 59028 KB Output is correct
3 Correct 34 ms 59024 KB Output is correct
4 Correct 27 ms 58944 KB Output is correct
5 Correct 28 ms 58964 KB Output is correct
6 Correct 27 ms 58964 KB Output is correct
7 Correct 29 ms 58916 KB Output is correct
8 Correct 29 ms 59080 KB Output is correct
9 Correct 28 ms 58964 KB Output is correct
10 Correct 29 ms 58940 KB Output is correct
11 Correct 28 ms 58964 KB Output is correct
12 Correct 28 ms 59028 KB Output is correct
13 Correct 29 ms 59008 KB Output is correct
14 Correct 29 ms 59092 KB Output is correct
15 Correct 29 ms 58964 KB Output is correct
16 Correct 29 ms 59028 KB Output is correct
17 Correct 28 ms 58984 KB Output is correct
18 Correct 30 ms 58964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 58964 KB Output is correct
2 Correct 27 ms 59028 KB Output is correct
3 Correct 34 ms 59024 KB Output is correct
4 Correct 27 ms 58944 KB Output is correct
5 Correct 28 ms 58964 KB Output is correct
6 Correct 27 ms 58964 KB Output is correct
7 Correct 29 ms 58916 KB Output is correct
8 Correct 29 ms 59080 KB Output is correct
9 Correct 28 ms 58964 KB Output is correct
10 Correct 29 ms 58940 KB Output is correct
11 Correct 28 ms 58964 KB Output is correct
12 Correct 28 ms 59028 KB Output is correct
13 Correct 29 ms 59008 KB Output is correct
14 Correct 29 ms 59092 KB Output is correct
15 Correct 29 ms 58964 KB Output is correct
16 Correct 29 ms 59028 KB Output is correct
17 Correct 28 ms 58984 KB Output is correct
18 Correct 30 ms 58964 KB Output is correct
19 Correct 28 ms 58964 KB Output is correct
20 Correct 26 ms 59112 KB Output is correct
21 Correct 28 ms 58964 KB Output is correct
22 Correct 28 ms 59056 KB Output is correct
23 Correct 30 ms 59008 KB Output is correct
24 Correct 33 ms 58940 KB Output is correct
25 Correct 30 ms 59024 KB Output is correct
26 Correct 29 ms 59008 KB Output is correct
27 Correct 28 ms 59016 KB Output is correct
28 Correct 29 ms 59028 KB Output is correct
29 Correct 30 ms 59156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 58964 KB Output is correct
2 Correct 27 ms 59028 KB Output is correct
3 Correct 34 ms 59024 KB Output is correct
4 Correct 27 ms 58944 KB Output is correct
5 Correct 28 ms 58964 KB Output is correct
6 Correct 27 ms 58964 KB Output is correct
7 Correct 29 ms 58916 KB Output is correct
8 Correct 29 ms 59080 KB Output is correct
9 Correct 28 ms 58964 KB Output is correct
10 Correct 29 ms 58940 KB Output is correct
11 Correct 28 ms 58964 KB Output is correct
12 Correct 28 ms 59028 KB Output is correct
13 Correct 29 ms 59008 KB Output is correct
14 Correct 29 ms 59092 KB Output is correct
15 Correct 29 ms 58964 KB Output is correct
16 Correct 29 ms 59028 KB Output is correct
17 Correct 28 ms 58984 KB Output is correct
18 Correct 30 ms 58964 KB Output is correct
19 Correct 28 ms 58964 KB Output is correct
20 Correct 26 ms 59112 KB Output is correct
21 Correct 28 ms 58964 KB Output is correct
22 Correct 28 ms 59056 KB Output is correct
23 Correct 30 ms 59008 KB Output is correct
24 Correct 33 ms 58940 KB Output is correct
25 Correct 30 ms 59024 KB Output is correct
26 Correct 29 ms 59008 KB Output is correct
27 Correct 28 ms 59016 KB Output is correct
28 Correct 29 ms 59028 KB Output is correct
29 Correct 30 ms 59156 KB Output is correct
30 Correct 425 ms 86588 KB Output is correct
31 Correct 401 ms 87872 KB Output is correct
32 Correct 160 ms 105768 KB Output is correct
33 Execution timed out 2062 ms 77368 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 32 ms 58964 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 58964 KB Output is correct
2 Correct 27 ms 59028 KB Output is correct
3 Correct 34 ms 59024 KB Output is correct
4 Correct 27 ms 58944 KB Output is correct
5 Correct 28 ms 58964 KB Output is correct
6 Correct 27 ms 58964 KB Output is correct
7 Correct 29 ms 58916 KB Output is correct
8 Correct 29 ms 59080 KB Output is correct
9 Correct 28 ms 58964 KB Output is correct
10 Correct 29 ms 58940 KB Output is correct
11 Correct 28 ms 58964 KB Output is correct
12 Correct 28 ms 59028 KB Output is correct
13 Correct 29 ms 59008 KB Output is correct
14 Correct 29 ms 59092 KB Output is correct
15 Correct 29 ms 58964 KB Output is correct
16 Correct 29 ms 59028 KB Output is correct
17 Correct 28 ms 58984 KB Output is correct
18 Correct 30 ms 58964 KB Output is correct
19 Correct 28 ms 58964 KB Output is correct
20 Correct 26 ms 59112 KB Output is correct
21 Correct 28 ms 58964 KB Output is correct
22 Correct 28 ms 59056 KB Output is correct
23 Correct 30 ms 59008 KB Output is correct
24 Correct 33 ms 58940 KB Output is correct
25 Correct 30 ms 59024 KB Output is correct
26 Correct 29 ms 59008 KB Output is correct
27 Correct 28 ms 59016 KB Output is correct
28 Correct 29 ms 59028 KB Output is correct
29 Correct 30 ms 59156 KB Output is correct
30 Correct 425 ms 86588 KB Output is correct
31 Correct 401 ms 87872 KB Output is correct
32 Correct 160 ms 105768 KB Output is correct
33 Execution timed out 2062 ms 77368 KB Time limit exceeded
34 Halted 0 ms 0 KB -