Submission #521327

# Submission time Handle Problem Language Result Execution time Memory
521327 2022-02-01T17:14:54 Z ymm Ancient Books (IOI17_books) C++17
100 / 100
683 ms 77672 KB
///
///   Oh? You're approaching me?
///

#include <bits/stdc++.h>
#define Loop(x,l,r) for(ll x = ll(l); x < ll(r); ++x)
#define LoopR(x,l,r) for(ll x = ll(r)-1; x >= ll(l); --x)
#define Kill(x) exit((cout << (x) << '\n', 0))
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
using namespace std;

#include "books.h"

const int N = 1'000'010;
int col[N]; int cnt=0;
int mn[N], mx[N];

ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	fill(col,col+N,-1);
	fill(mn,mn+N,N);
	fill(mx,mx+N,0);
	cnt=0;
	Loop(i,0,n){
		if(col[i]<0){
			int j = i;
			while(col[j]<0){
				mn[cnt] = min(mn[cnt], j);
				mx[cnt] = max(mx[cnt], j);
				col[j] = cnt;
				j = p[j];
			}
			++cnt;
		}
	}
	int l=0, r=n-1;
	while(l<s && p[l]==l) ++l;
	while(r>s && p[r]==r) --r;
//	Loop(i,0,n) cout << col[i] << ' '; cout << '\n';
//	Loop(i,0,cnt) cout << mx[i] << ' '; cout << '\n';
//	Loop(i,0,cnt) cout << mn[i] << ' '; cout << '\n';
	int droot=0;
	// bfs
	{
		vector<int> dis(n, N);
		set<int> cand; Loop(i,l,r+1) cand.insert(i);
		deque<pii> q; q.emplace_back(col[s], 0); dis[col[s]]=0;
		while(q.size()){
			auto [v, d] = q.front(); q.pop_front();
			if(d != dis[v]) continue;
			int mn = ::mn[v], mx = ::mx[v];
			if(mn<=s&&s<=mx) droot=d;
			for(;;){
				int i;
				auto it = cand.lower_bound(mn);
				if(it == cand.end() || (i = *it) > mx) break;
//				cout<<i<<"!\n";
				cand.erase(it);
				if(d < dis[i = col[i]]){
					dis[i] = d;
					q.emplace_front(i, d);
				}
			}
			if(l<mn && d+1<dis[col[mn-1]]){
				dis[col[mn-1]] = d+1;
				q.emplace_back(col[mn-1], d+1);
			}
			if(mx<r && d+1<dis[col[mx+1]]){
				dis[col[mx+1]] = d+1;
				q.emplace_back(col[mx+1], d+1);
			}
		}
	}
//	cout << mnrt << ' ' << mxrt << ' ' << droot << '\n';
	ll ans = 0;
	Loop(i,l,r+1) ans += abs(i-p[i]);
	int foo=0;
	Loop(i,l,r+1) {
		droot += s<i && !foo;
		foo += i==mn[col[i]];
		foo -= i==mx[col[i]];
	}
	foo=0;
	LoopR(i,l,r+1){
		droot += i<s && !foo;
		foo += i==mx[col[i]];
		foo -= i==mn[col[i]];
	}
	return ans+2*droot;
}

//int main()
//{
//	/*vector<int> p = {0, 1, 2, 3};
//	do{
//		if(minimum_walk(p,0)==8){
//			for(int x : p) cout << x << ' ';
//			cout << '\n';
//		}
//	}while(next_permutation(p.begin(), p.end()));*/
//	cout << minimum_walk({5, 1, 2, 3, 4, 0}, 3) << '\n';
//}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 6 ms 12008 KB Output is correct
6 Correct 6 ms 12016 KB Output is correct
7 Correct 6 ms 12004 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12096 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 6 ms 11980 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 5 ms 11980 KB Output is correct
17 Correct 6 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 6 ms 12008 KB Output is correct
6 Correct 6 ms 12016 KB Output is correct
7 Correct 6 ms 12004 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12096 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 6 ms 11980 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 5 ms 11980 KB Output is correct
17 Correct 6 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 8 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 6 ms 11980 KB Output is correct
26 Correct 6 ms 11980 KB Output is correct
27 Correct 6 ms 11980 KB Output is correct
28 Correct 6 ms 11980 KB Output is correct
29 Correct 5 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 6 ms 12008 KB Output is correct
6 Correct 6 ms 12016 KB Output is correct
7 Correct 6 ms 12004 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12096 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 6 ms 11980 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 5 ms 11980 KB Output is correct
17 Correct 6 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 8 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 6 ms 11980 KB Output is correct
26 Correct 6 ms 11980 KB Output is correct
27 Correct 6 ms 11980 KB Output is correct
28 Correct 6 ms 11980 KB Output is correct
29 Correct 5 ms 11980 KB Output is correct
30 Correct 630 ms 70764 KB Output is correct
31 Correct 487 ms 70956 KB Output is correct
32 Correct 115 ms 23952 KB Output is correct
33 Correct 565 ms 70732 KB Output is correct
34 Correct 498 ms 70784 KB Output is correct
35 Correct 501 ms 70944 KB Output is correct
36 Correct 515 ms 70796 KB Output is correct
37 Correct 445 ms 70824 KB Output is correct
38 Correct 505 ms 70820 KB Output is correct
39 Correct 446 ms 70868 KB Output is correct
40 Correct 460 ms 70808 KB Output is correct
41 Correct 481 ms 70884 KB Output is correct
42 Correct 462 ms 70816 KB Output is correct
43 Correct 472 ms 70756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 6 ms 11980 KB Output is correct
3 Correct 6 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 5 ms 12076 KB Output is correct
6 Correct 6 ms 11980 KB Output is correct
7 Correct 5 ms 12064 KB Output is correct
8 Correct 6 ms 12004 KB Output is correct
9 Correct 5 ms 11980 KB Output is correct
10 Correct 6 ms 12108 KB Output is correct
11 Correct 6 ms 12072 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 8 ms 12076 KB Output is correct
14 Correct 6 ms 12072 KB Output is correct
15 Correct 8 ms 12072 KB Output is correct
16 Correct 6 ms 12076 KB Output is correct
17 Correct 6 ms 11980 KB Output is correct
18 Correct 5 ms 11980 KB Output is correct
19 Correct 6 ms 12076 KB Output is correct
20 Correct 6 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 6 ms 12008 KB Output is correct
6 Correct 6 ms 12016 KB Output is correct
7 Correct 6 ms 12004 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 6 ms 11980 KB Output is correct
10 Correct 5 ms 12096 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 6 ms 11980 KB Output is correct
13 Correct 6 ms 11980 KB Output is correct
14 Correct 6 ms 11980 KB Output is correct
15 Correct 6 ms 11980 KB Output is correct
16 Correct 5 ms 11980 KB Output is correct
17 Correct 6 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
20 Correct 8 ms 11980 KB Output is correct
21 Correct 6 ms 11980 KB Output is correct
22 Correct 6 ms 11980 KB Output is correct
23 Correct 5 ms 11980 KB Output is correct
24 Correct 6 ms 11980 KB Output is correct
25 Correct 6 ms 11980 KB Output is correct
26 Correct 6 ms 11980 KB Output is correct
27 Correct 6 ms 11980 KB Output is correct
28 Correct 6 ms 11980 KB Output is correct
29 Correct 5 ms 11980 KB Output is correct
30 Correct 630 ms 70764 KB Output is correct
31 Correct 487 ms 70956 KB Output is correct
32 Correct 115 ms 23952 KB Output is correct
33 Correct 565 ms 70732 KB Output is correct
34 Correct 498 ms 70784 KB Output is correct
35 Correct 501 ms 70944 KB Output is correct
36 Correct 515 ms 70796 KB Output is correct
37 Correct 445 ms 70824 KB Output is correct
38 Correct 505 ms 70820 KB Output is correct
39 Correct 446 ms 70868 KB Output is correct
40 Correct 460 ms 70808 KB Output is correct
41 Correct 481 ms 70884 KB Output is correct
42 Correct 462 ms 70816 KB Output is correct
43 Correct 472 ms 70756 KB Output is correct
44 Correct 6 ms 11980 KB Output is correct
45 Correct 6 ms 11980 KB Output is correct
46 Correct 6 ms 11980 KB Output is correct
47 Correct 6 ms 11980 KB Output is correct
48 Correct 5 ms 12076 KB Output is correct
49 Correct 6 ms 11980 KB Output is correct
50 Correct 5 ms 12064 KB Output is correct
51 Correct 6 ms 12004 KB Output is correct
52 Correct 5 ms 11980 KB Output is correct
53 Correct 6 ms 12108 KB Output is correct
54 Correct 6 ms 12072 KB Output is correct
55 Correct 7 ms 11980 KB Output is correct
56 Correct 8 ms 12076 KB Output is correct
57 Correct 6 ms 12072 KB Output is correct
58 Correct 8 ms 12072 KB Output is correct
59 Correct 6 ms 12076 KB Output is correct
60 Correct 6 ms 11980 KB Output is correct
61 Correct 5 ms 11980 KB Output is correct
62 Correct 6 ms 12076 KB Output is correct
63 Correct 6 ms 11980 KB Output is correct
64 Correct 503 ms 77512 KB Output is correct
65 Correct 547 ms 77580 KB Output is correct
66 Correct 524 ms 77508 KB Output is correct
67 Correct 521 ms 77536 KB Output is correct
68 Correct 43 ms 18544 KB Output is correct
69 Correct 47 ms 18472 KB Output is correct
70 Correct 44 ms 18500 KB Output is correct
71 Correct 46 ms 18392 KB Output is correct
72 Correct 43 ms 18460 KB Output is correct
73 Correct 42 ms 18500 KB Output is correct
74 Correct 602 ms 77424 KB Output is correct
75 Correct 540 ms 77536 KB Output is correct
76 Correct 485 ms 77488 KB Output is correct
77 Correct 462 ms 77588 KB Output is correct
78 Correct 510 ms 77484 KB Output is correct
79 Correct 526 ms 77508 KB Output is correct
80 Correct 116 ms 30508 KB Output is correct
81 Correct 582 ms 77508 KB Output is correct
82 Correct 553 ms 77580 KB Output is correct
83 Correct 563 ms 77476 KB Output is correct
84 Correct 539 ms 77672 KB Output is correct
85 Correct 602 ms 77476 KB Output is correct
86 Correct 605 ms 77376 KB Output is correct
87 Correct 683 ms 77548 KB Output is correct
88 Correct 655 ms 77360 KB Output is correct
89 Correct 567 ms 77484 KB Output is correct
90 Correct 528 ms 77488 KB Output is correct
91 Correct 518 ms 77476 KB Output is correct
92 Correct 542 ms 77540 KB Output is correct