Submission #1029092

# Submission time Handle Problem Language Result Execution time Memory
1029092 2024-07-20T12:19:38 Z AmirAli_H1 Ancient Books (IOI17_books) C++17
100 / 100
164 ms 32852 KB
// In the name of Allah
 
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
 
typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;
 
#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int maxn = 1e6 + 4;
const int oo = 1e9 + 7;
 
int n; ll res;
int A[maxn], mark[maxn];
int sm[maxn], mn, mx;
int dis[maxn]; deque<int> dq;
 
void dfs(int v) {
	mark[v] = 1;
	mn = min(mn, v); mx = max(mx, v);
	if (!mark[A[v]]) dfs(A[v]);
}

void bfs01(int s) {
	fill(mark, mark + n, 0); fill(dis, dis + n, oo);
	dis[s] = 0; dq.pb(s);
	while (!dq.empty()) {
		int v = dq.front(); dq.pop_front();
		if (mark[v]) continue;
		mark[v] = 1;
		if (v - 1 >= 0) {
			int u = v - 1, w = (abs(v - s) < abs(u - s));
			if (dis[v] + w < dis[u]) {
				dis[u] = dis[v] + w;
				if (w == 0) dq.push_front(u);
				else dq.pb(u);
			}
		}
		if (v + 1 < n) {
			int u = v + 1, w = (abs(v - s) < abs(u - s));
			if (dis[v] + w < dis[u]) {
				dis[u] = dis[v] + w;
				if (w == 0) dq.push_front(u);
				else dq.pb(u);
			}
		}
		int u = A[v], w = 0;
		if (dis[v] + w < dis[u]) {
			dis[u] = dis[v] + w;
			if (w == 0) dq.push_front(u);
			else dq.pb(u);
		}
	}
}
 
ll minimum_walk(vector<int> p, int s) {
	n = len(p); res = 0;
	for (int i = 0; i < n; i++) {
		A[i] = p[i];
		res += abs(i - A[i]);
	}
	
	int m1 = s, m2 = s;
	for (int i = 0; i < n; i++) {
		if (A[i] != i) {
			m1 = min(m1, i); m2 = max(m2, i);
		}
		
		if (!mark[i]) {
			mn = i; mx = i;
			dfs(i);
			sm[mn]++; sm[mx]--;
		}
	}
	for (int i = 1; i < n; i++) {
		sm[i] += sm[i - 1];
	}
	
	int l = s, r = s;
	for (int i = s; i < m2; i++) {
		if (sm[i] != 0) r = i + 1;
		else break;
	}
	for (int i = s - 1; i >= m1; i--) {
		if (sm[i] != 0) l = i;
		else break;
	}
	
	for (int i = m1; i < m2; i++) {
		if (sm[i] == 0) res += 2;
	}
	
	bfs01(s);
	res += 2 * dis[l];
	return res;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:95:13: warning: variable 'r' set but not used [-Wunused-but-set-variable]
   95 |  int l = s, r = s;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 448 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 452 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 448 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 452 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 135 ms 32852 KB Output is correct
31 Correct 164 ms 32744 KB Output is correct
32 Correct 117 ms 30460 KB Output is correct
33 Correct 108 ms 30580 KB Output is correct
34 Correct 96 ms 30548 KB Output is correct
35 Correct 141 ms 30448 KB Output is correct
36 Correct 109 ms 30548 KB Output is correct
37 Correct 99 ms 30576 KB Output is correct
38 Correct 111 ms 30448 KB Output is correct
39 Correct 111 ms 30576 KB Output is correct
40 Correct 126 ms 30572 KB Output is correct
41 Correct 140 ms 31824 KB Output is correct
42 Correct 122 ms 31360 KB Output is correct
43 Correct 108 ms 31472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 448 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 448 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 452 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 135 ms 32852 KB Output is correct
31 Correct 164 ms 32744 KB Output is correct
32 Correct 117 ms 30460 KB Output is correct
33 Correct 108 ms 30580 KB Output is correct
34 Correct 96 ms 30548 KB Output is correct
35 Correct 141 ms 30448 KB Output is correct
36 Correct 109 ms 30548 KB Output is correct
37 Correct 99 ms 30576 KB Output is correct
38 Correct 111 ms 30448 KB Output is correct
39 Correct 111 ms 30576 KB Output is correct
40 Correct 126 ms 30572 KB Output is correct
41 Correct 140 ms 31824 KB Output is correct
42 Correct 122 ms 31360 KB Output is correct
43 Correct 108 ms 31472 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 448 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 2396 KB Output is correct
56 Correct 0 ms 2396 KB Output is correct
57 Correct 1 ms 2396 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 604 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 94 ms 30804 KB Output is correct
65 Correct 101 ms 30840 KB Output is correct
66 Correct 98 ms 30848 KB Output is correct
67 Correct 97 ms 31624 KB Output is correct
68 Correct 9 ms 4956 KB Output is correct
69 Correct 11 ms 3224 KB Output is correct
70 Correct 9 ms 3280 KB Output is correct
71 Correct 11 ms 3264 KB Output is correct
72 Correct 9 ms 3272 KB Output is correct
73 Correct 10 ms 5192 KB Output is correct
74 Correct 102 ms 30444 KB Output is correct
75 Correct 98 ms 30596 KB Output is correct
76 Correct 117 ms 31572 KB Output is correct
77 Correct 106 ms 31492 KB Output is correct
78 Correct 98 ms 30836 KB Output is correct
79 Correct 114 ms 30732 KB Output is correct
80 Correct 85 ms 30460 KB Output is correct
81 Correct 88 ms 30588 KB Output is correct
82 Correct 86 ms 30464 KB Output is correct
83 Correct 99 ms 30548 KB Output is correct
84 Correct 96 ms 30548 KB Output is correct
85 Correct 105 ms 30604 KB Output is correct
86 Correct 95 ms 30580 KB Output is correct
87 Correct 84 ms 30480 KB Output is correct
88 Correct 125 ms 30552 KB Output is correct
89 Correct 112 ms 30460 KB Output is correct
90 Correct 91 ms 30604 KB Output is correct
91 Correct 108 ms 30548 KB Output is correct
92 Correct 99 ms 30548 KB Output is correct