Submission #935161

# Submission time Handle Problem Language Result Execution time Memory
935161 2024-02-28T18:08:35 Z Lobo Ancient Books (IOI17_books) C++17
100 / 100
184 ms 187356 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e6+10;

int n;
int ds[maxn], dsz[maxn], dsl[maxn], dsr[maxn];
int nxr[maxn], costr[maxn];
int nxl[maxn], costl[maxn];

int find(int v) {
	if(v == ds[v]) return v;
	return ds[v] = find(ds[v]);
}

void join(int u, int v) {
	u = find(u);
	v = find(v);
	if(u == v) return;

	if(dsz[u] < dsz[v]) swap(u,v);
	dsz[u]+= dsz[v];
	dsl[u] = min(dsl[u],dsl[v]);
	dsr[u] = max(dsr[u],dsr[v]);
	ds[v] = u;
}

int L,R;
unordered_map<int,int> dp[maxn];
int sol(int l, int r) {
	if(dp[l].count(r)) return dp[l][r];
	if(l == L && r == R) return dp[l][r] = 0;

	dp[l][r] = inf;
	if(l > L) dp[l][r] = min(dp[l][r],2*costl[find(l)]+sol(dsl[find(nxl[find(l)])],max(r,dsr[find(nxl[find(l)])])));
	if(r < R) dp[l][r] = min(dp[l][r],2*costr[find(r)]+sol(min(l,dsl[find(nxr[find(r)])]),dsr[find(nxr[find(r)])]));
	return dp[l][r];
}

long long minimum_walk(vector<int32_t> p, int32_t s) {
	// return 0;

	n = p.size();

	for(int i = 0; i < n; i++) {
		ds[i] = i;
		dsz[i] = 1;
		dsl[i] = i;
		dsr[i] = i;
	}

	for(int i = 0; i < n; i++) {
		join(i,p[i]);
	}

	int itl = s;
	while(itl != n) {
		int itr = itl;
		int cur = itl;
		while(cur <= itr) {
			itr = max(itr,dsr[find(cur)]);
			cur++;
		}
		for(int i = itl; i < itr; i++) {
			join(i,i+1);
		}
		itl = itr+1;
	}

	int itr = s;
	while(itr >= 0) {
		int itl = itr;
		int cur = itr;
		while(cur >= itl) {
			itl = min(itl,dsl[find(cur)]);
			cur--;
		}
		for(int i = itl; i < itr; i++) {
			join(i,i+1);
		}
		itr = itl-1;
	}

	L = 0;
	R = n-1;
	while(L < s && dsz[find(L)] == 1) L++;
	while(R > s && dsz[find(R)] == 1) R--;

	nxr[find(R)] = R;
	costr[find(R)] = 0;
	for(int i = R-1; i >= s; i--) {
		if(find(i) == find(i+1)) {
			continue;
		}
		else if(dsl[find(i+1)] < s or dsr[find(i+1)] == R) {
			nxr[find(i)] = i+1;
			costr[find(i)] = 1;
		}
		else {
			nxr[find(i)] = nxr[find(i+1)];
			costr[find(i)] = costr[find(i+1)]+1;
		}
	}

	nxl[find(L)] = L;
	costl[find(L)] = 0;
	for(int i = L+1; i <= s; i++) {
		if(find(i) == find(i-1)) {
			continue;
		}
		else if(dsr[find(i-1)] > s or dsl[find(i-1)] == L) {
			nxl[find(i)] = i-1;
			costl[find(i)] = 1;
		}
		else {
			nxl[find(i)] = nxl[find(i-1)];
			costl[find(i)] = costl[find(i-1)]+1;
		}
	}

	int ans = 0;
	for(int i = 0; i < n; i++) ans+= abs(p[i]-i);

	return ans+sol(dsl[find(s)],dsr[find(s)]);
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 70232 KB Output is correct
2 Correct 17 ms 70236 KB Output is correct
3 Correct 16 ms 70384 KB Output is correct
4 Correct 17 ms 70236 KB Output is correct
5 Correct 16 ms 70236 KB Output is correct
6 Correct 17 ms 70236 KB Output is correct
7 Correct 16 ms 70236 KB Output is correct
8 Correct 17 ms 70236 KB Output is correct
9 Correct 16 ms 70236 KB Output is correct
10 Correct 17 ms 70236 KB Output is correct
11 Correct 16 ms 70240 KB Output is correct
12 Correct 17 ms 70392 KB Output is correct
13 Correct 17 ms 70248 KB Output is correct
14 Correct 17 ms 70232 KB Output is correct
15 Correct 16 ms 70372 KB Output is correct
16 Correct 17 ms 70276 KB Output is correct
17 Correct 17 ms 70236 KB Output is correct
18 Correct 17 ms 70380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 70232 KB Output is correct
2 Correct 17 ms 70236 KB Output is correct
3 Correct 16 ms 70384 KB Output is correct
4 Correct 17 ms 70236 KB Output is correct
5 Correct 16 ms 70236 KB Output is correct
6 Correct 17 ms 70236 KB Output is correct
7 Correct 16 ms 70236 KB Output is correct
8 Correct 17 ms 70236 KB Output is correct
9 Correct 16 ms 70236 KB Output is correct
10 Correct 17 ms 70236 KB Output is correct
11 Correct 16 ms 70240 KB Output is correct
12 Correct 17 ms 70392 KB Output is correct
13 Correct 17 ms 70248 KB Output is correct
14 Correct 17 ms 70232 KB Output is correct
15 Correct 16 ms 70372 KB Output is correct
16 Correct 17 ms 70276 KB Output is correct
17 Correct 17 ms 70236 KB Output is correct
18 Correct 17 ms 70380 KB Output is correct
19 Correct 18 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 17 ms 70232 KB Output is correct
22 Correct 17 ms 70236 KB Output is correct
23 Correct 17 ms 70232 KB Output is correct
24 Correct 17 ms 70356 KB Output is correct
25 Correct 17 ms 70232 KB Output is correct
26 Correct 17 ms 70236 KB Output is correct
27 Correct 17 ms 70236 KB Output is correct
28 Correct 18 ms 70232 KB Output is correct
29 Correct 17 ms 70236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 70232 KB Output is correct
2 Correct 17 ms 70236 KB Output is correct
3 Correct 16 ms 70384 KB Output is correct
4 Correct 17 ms 70236 KB Output is correct
5 Correct 16 ms 70236 KB Output is correct
6 Correct 17 ms 70236 KB Output is correct
7 Correct 16 ms 70236 KB Output is correct
8 Correct 17 ms 70236 KB Output is correct
9 Correct 16 ms 70236 KB Output is correct
10 Correct 17 ms 70236 KB Output is correct
11 Correct 16 ms 70240 KB Output is correct
12 Correct 17 ms 70392 KB Output is correct
13 Correct 17 ms 70248 KB Output is correct
14 Correct 17 ms 70232 KB Output is correct
15 Correct 16 ms 70372 KB Output is correct
16 Correct 17 ms 70276 KB Output is correct
17 Correct 17 ms 70236 KB Output is correct
18 Correct 17 ms 70380 KB Output is correct
19 Correct 18 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 17 ms 70232 KB Output is correct
22 Correct 17 ms 70236 KB Output is correct
23 Correct 17 ms 70232 KB Output is correct
24 Correct 17 ms 70356 KB Output is correct
25 Correct 17 ms 70232 KB Output is correct
26 Correct 17 ms 70236 KB Output is correct
27 Correct 17 ms 70236 KB Output is correct
28 Correct 18 ms 70232 KB Output is correct
29 Correct 17 ms 70236 KB Output is correct
30 Correct 170 ms 105044 KB Output is correct
31 Correct 180 ms 103088 KB Output is correct
32 Correct 102 ms 102992 KB Output is correct
33 Correct 115 ms 113168 KB Output is correct
34 Correct 115 ms 113164 KB Output is correct
35 Correct 119 ms 113232 KB Output is correct
36 Correct 118 ms 113240 KB Output is correct
37 Correct 122 ms 113168 KB Output is correct
38 Correct 121 ms 113176 KB Output is correct
39 Correct 122 ms 113328 KB Output is correct
40 Correct 127 ms 113164 KB Output is correct
41 Correct 143 ms 111192 KB Output is correct
42 Correct 139 ms 113180 KB Output is correct
43 Correct 110 ms 102944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 70232 KB Output is correct
2 Correct 16 ms 70232 KB Output is correct
3 Correct 15 ms 70236 KB Output is correct
4 Correct 16 ms 70236 KB Output is correct
5 Correct 16 ms 70236 KB Output is correct
6 Correct 16 ms 70392 KB Output is correct
7 Correct 17 ms 70236 KB Output is correct
8 Correct 17 ms 70236 KB Output is correct
9 Correct 16 ms 70236 KB Output is correct
10 Correct 16 ms 70332 KB Output is correct
11 Correct 16 ms 70236 KB Output is correct
12 Correct 16 ms 70688 KB Output is correct
13 Correct 16 ms 70236 KB Output is correct
14 Correct 17 ms 70232 KB Output is correct
15 Correct 16 ms 70388 KB Output is correct
16 Correct 16 ms 70232 KB Output is correct
17 Correct 16 ms 70236 KB Output is correct
18 Correct 16 ms 70364 KB Output is correct
19 Correct 15 ms 70236 KB Output is correct
20 Correct 16 ms 70488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 70232 KB Output is correct
2 Correct 17 ms 70236 KB Output is correct
3 Correct 16 ms 70384 KB Output is correct
4 Correct 17 ms 70236 KB Output is correct
5 Correct 16 ms 70236 KB Output is correct
6 Correct 17 ms 70236 KB Output is correct
7 Correct 16 ms 70236 KB Output is correct
8 Correct 17 ms 70236 KB Output is correct
9 Correct 16 ms 70236 KB Output is correct
10 Correct 17 ms 70236 KB Output is correct
11 Correct 16 ms 70240 KB Output is correct
12 Correct 17 ms 70392 KB Output is correct
13 Correct 17 ms 70248 KB Output is correct
14 Correct 17 ms 70232 KB Output is correct
15 Correct 16 ms 70372 KB Output is correct
16 Correct 17 ms 70276 KB Output is correct
17 Correct 17 ms 70236 KB Output is correct
18 Correct 17 ms 70380 KB Output is correct
19 Correct 18 ms 70236 KB Output is correct
20 Correct 17 ms 70236 KB Output is correct
21 Correct 17 ms 70232 KB Output is correct
22 Correct 17 ms 70236 KB Output is correct
23 Correct 17 ms 70232 KB Output is correct
24 Correct 17 ms 70356 KB Output is correct
25 Correct 17 ms 70232 KB Output is correct
26 Correct 17 ms 70236 KB Output is correct
27 Correct 17 ms 70236 KB Output is correct
28 Correct 18 ms 70232 KB Output is correct
29 Correct 17 ms 70236 KB Output is correct
30 Correct 170 ms 105044 KB Output is correct
31 Correct 180 ms 103088 KB Output is correct
32 Correct 102 ms 102992 KB Output is correct
33 Correct 115 ms 113168 KB Output is correct
34 Correct 115 ms 113164 KB Output is correct
35 Correct 119 ms 113232 KB Output is correct
36 Correct 118 ms 113240 KB Output is correct
37 Correct 122 ms 113168 KB Output is correct
38 Correct 121 ms 113176 KB Output is correct
39 Correct 122 ms 113328 KB Output is correct
40 Correct 127 ms 113164 KB Output is correct
41 Correct 143 ms 111192 KB Output is correct
42 Correct 139 ms 113180 KB Output is correct
43 Correct 110 ms 102944 KB Output is correct
44 Correct 16 ms 70232 KB Output is correct
45 Correct 16 ms 70232 KB Output is correct
46 Correct 15 ms 70236 KB Output is correct
47 Correct 16 ms 70236 KB Output is correct
48 Correct 16 ms 70236 KB Output is correct
49 Correct 16 ms 70392 KB Output is correct
50 Correct 17 ms 70236 KB Output is correct
51 Correct 17 ms 70236 KB Output is correct
52 Correct 16 ms 70236 KB Output is correct
53 Correct 16 ms 70332 KB Output is correct
54 Correct 16 ms 70236 KB Output is correct
55 Correct 16 ms 70688 KB Output is correct
56 Correct 16 ms 70236 KB Output is correct
57 Correct 17 ms 70232 KB Output is correct
58 Correct 16 ms 70388 KB Output is correct
59 Correct 16 ms 70232 KB Output is correct
60 Correct 16 ms 70236 KB Output is correct
61 Correct 16 ms 70364 KB Output is correct
62 Correct 15 ms 70236 KB Output is correct
63 Correct 16 ms 70488 KB Output is correct
64 Correct 107 ms 113168 KB Output is correct
65 Correct 109 ms 117328 KB Output is correct
66 Correct 125 ms 117264 KB Output is correct
67 Correct 125 ms 121632 KB Output is correct
68 Correct 27 ms 75100 KB Output is correct
69 Correct 26 ms 75244 KB Output is correct
70 Correct 27 ms 75244 KB Output is correct
71 Correct 27 ms 75096 KB Output is correct
72 Correct 25 ms 75100 KB Output is correct
73 Correct 26 ms 75100 KB Output is correct
74 Correct 124 ms 115280 KB Output is correct
75 Correct 115 ms 117336 KB Output is correct
76 Correct 118 ms 107088 KB Output is correct
77 Correct 114 ms 113236 KB Output is correct
78 Correct 120 ms 121360 KB Output is correct
79 Correct 118 ms 125472 KB Output is correct
80 Correct 98 ms 102948 KB Output is correct
81 Correct 182 ms 187220 KB Output is correct
82 Correct 184 ms 187356 KB Output is correct
83 Correct 133 ms 127412 KB Output is correct
84 Correct 134 ms 125776 KB Output is correct
85 Correct 133 ms 123004 KB Output is correct
86 Correct 129 ms 121940 KB Output is correct
87 Correct 153 ms 152864 KB Output is correct
88 Correct 148 ms 140052 KB Output is correct
89 Correct 143 ms 129828 KB Output is correct
90 Correct 128 ms 121628 KB Output is correct
91 Correct 128 ms 121360 KB Output is correct
92 Correct 126 ms 121424 KB Output is correct