Submission #271591

# Submission time Handle Problem Language Result Execution time Memory
271591 2020-08-18T06:52:33 Z kdh9949 Ancient Books (IOI17_books) C++17
100 / 100
1418 ms 123976 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) v.begin(),v.end()

ll minimum_walk(vint p, int s) {
	int n = p.size();
	
	vint cnum(n), lp(1), rp(1);
	int cnt = 0;
	ll ans = 0;
	for(int i = 0; i < n; i++) {
		if(cnum[i] || p[i] == i) continue;
		cnt++;
		int x = i;
		lp.push_back(i);
		rp.push_back(i);
		do {
			ans += abs(x - p[x]);
			x = p[x];
			lp[cnt] = min(lp[cnt], x);
			rp[cnt] = max(rp[cnt], x);
			cnum[x] = cnt;
		} while(x != i);
	}

	int ls = s, rs = s;	
	vpii seg;
	for(int i = 1; i <= cnt; i++) seg.emplace_back(lp[i], rp[i]);
	sort(all(seg), [&](pii &p, pii &q){ return p.y > q.y; });
	for(pii &p : seg) if(ls <= p.y) ls = min(ls, p.x);
	sort(all(seg), [&](pii &p, pii &q){ return p.x < q.x; });
	for(pii &p : seg) if(p.x <= rs) rs = max(rs, p.y);

	vint d(n, n);
	priority_queue<pii, vector<pii>, greater<pii>> pq;
	set<int> cand[2];
	for(int i = ls; i <= rs; i++) { cand[0].insert(i); cand[1].insert(i); }
	
	auto put = [&](int l, int r, int c, int a) {
		auto &cc = cand[a];
		auto it = cc.lower_bound(l);
		while(it != cc.end() && *it <= r) {
			int x = *it;
			it++;
			cc.erase(x);
			if(d[x] > c + a) {
				d[x] = c + a;
				pq.emplace(c + a, x);
			}
		}
	};
	
	put(s, s, 0, 0);
	while(!pq.empty()) {
		pii c = pq.top();
		pq.pop();
		if(d[c.y] != c.x) continue;
		if(c.y == ls || c.y == rs) {
			ans += 2 * c.x;
			break;
		}
		if(cnum[c.y]) put(lp[cnum[c.y]], rp[cnum[c.y]], c.x, 0);
		if(c.y > 0) put(c.y - 1, c.y - 1, c.x, 1);
		if(c.y < n - 1) put(c.y + 1, c.y + 1, c.x, 1);
	}

	vpii lseg;
	for(int i = 1; i <= cnt; i++) if(rp[i] < ls) lseg.emplace_back(lp[i], rp[i]);
	sort(all(lseg), [&](pii &p, pii &q){ return p.y > q.y; });
	for(pii &p : lseg) {
		if(p.y < ls) ans += 2 * (ls - p.y);
		ls = min(ls, p.x);
	}

	vpii rseg;
	for(int i = 1; i <= cnt; i++) if(rs < lp[i]) rseg.emplace_back(lp[i], rp[i]);
	sort(all(rseg), [&](pii &p, pii &q){ return p.x < q.x; });
	for(pii &p : rseg) {
		if(rs < p.x) ans += 2 * (p.x - rs);
		rs = max(rs, p.y);
	}

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 819 ms 109888 KB Output is correct
31 Correct 845 ms 109968 KB Output is correct
32 Correct 167 ms 15992 KB Output is correct
33 Correct 199 ms 22028 KB Output is correct
34 Correct 193 ms 22028 KB Output is correct
35 Correct 196 ms 22412 KB Output is correct
36 Correct 202 ms 21516 KB Output is correct
37 Correct 184 ms 16952 KB Output is correct
38 Correct 183 ms 16316 KB Output is correct
39 Correct 180 ms 16248 KB Output is correct
40 Correct 196 ms 17144 KB Output is correct
41 Correct 347 ms 40440 KB Output is correct
42 Correct 242 ms 16120 KB Output is correct
43 Correct 781 ms 113908 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 1 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 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 819 ms 109888 KB Output is correct
31 Correct 845 ms 109968 KB Output is correct
32 Correct 167 ms 15992 KB Output is correct
33 Correct 199 ms 22028 KB Output is correct
34 Correct 193 ms 22028 KB Output is correct
35 Correct 196 ms 22412 KB Output is correct
36 Correct 202 ms 21516 KB Output is correct
37 Correct 184 ms 16952 KB Output is correct
38 Correct 183 ms 16316 KB Output is correct
39 Correct 180 ms 16248 KB Output is correct
40 Correct 196 ms 17144 KB Output is correct
41 Correct 347 ms 40440 KB Output is correct
42 Correct 242 ms 16120 KB Output is correct
43 Correct 781 ms 113908 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 0 ms 384 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 2 ms 384 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 2 ms 384 KB Output is correct
64 Correct 912 ms 119900 KB Output is correct
65 Correct 967 ms 117468 KB Output is correct
66 Correct 1418 ms 111360 KB Output is correct
67 Correct 1224 ms 114540 KB Output is correct
68 Correct 94 ms 12532 KB Output is correct
69 Correct 99 ms 11592 KB Output is correct
70 Correct 76 ms 12412 KB Output is correct
71 Correct 80 ms 12152 KB Output is correct
72 Correct 111 ms 11640 KB Output is correct
73 Correct 94 ms 12024 KB Output is correct
74 Correct 197 ms 21516 KB Output is correct
75 Correct 192 ms 21588 KB Output is correct
76 Correct 879 ms 122384 KB Output is correct
77 Correct 876 ms 123976 KB Output is correct
78 Correct 1147 ms 99200 KB Output is correct
79 Correct 1083 ms 101044 KB Output is correct
80 Correct 169 ms 15992 KB Output is correct
81 Correct 1359 ms 118084 KB Output is correct
82 Correct 1385 ms 117888 KB Output is correct
83 Correct 1401 ms 113924 KB Output is correct
84 Correct 1381 ms 113288 KB Output is correct
85 Correct 1336 ms 111780 KB Output is correct
86 Correct 1333 ms 110616 KB Output is correct
87 Correct 1333 ms 112332 KB Output is correct
88 Correct 1353 ms 114632 KB Output is correct
89 Correct 1384 ms 114304 KB Output is correct
90 Correct 1325 ms 110332 KB Output is correct
91 Correct 1349 ms 110140 KB Output is correct
92 Correct 1349 ms 110328 KB Output is correct