Submission #286905

# Submission time Handle Problem Language Result Execution time Memory
286905 2020-08-31T06:57:14 Z tmwilliamlin168 Ancient Books (IOI17_books) C++14
100 / 100
1147 ms 209380 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=1e6;
int n, a, b, who[mxN];
vector<int> adj[mxN], adj2[mxN], st, adj3[mxN], c;
bool vis[mxN];
ar<int, 3> dp[mxN];

void dfs1(int u) {
	vis[u]=1;
	for(int v : adj[u])
		if(!vis[v])
			dfs1(v);
	st.push_back(u);
}

void dfs2(int u, int r) {
	vis[u]=0;
	c.push_back(u);
	who[u]=r;
	for(int v : adj2[u])
		if(vis[v])
			dfs2(v, r);
}

ll minimum_walk(vector<int> p, int s) {
	n=p.size();
	ll ans=0;
	for(; a<s&&p[a]==a; ++a);
	for(b=n-1; b>s&&p[b]==b; --b);
	for(int i=a; i<s; ++i)
		adj[i].push_back(i+1);
	for(int i=b; i>s; --i)
		adj[i].push_back(i-1);
	for(int i=a; i<=b; ++i) {
		ans+=abs(i-p[i]);
		adj[i].push_back(p[i]);
	}
	for(int i=a; i<=b; ++i)
		if(!vis[i])
			dfs1(i);
	for(int i=a; i<=b; ++i)
		for(int j : adj[i])
			adj2[j].push_back(i);
	for(int i=b-a; ~i; --i) {
		if(!vis[st[i]])
			continue;
		dfs2(st[i], st[i]);
		dp[st[i]]={n, n, n};
		if(!vis[a]&&st[i]==who[a])
			dp[st[i]][0]=0;
		if(!vis[b]&&st[i]==who[b])
			dp[st[i]][1]=0;
		for(int u : c)
			for(int v : adj2[u])
				for(int k : {0, 1, 2})
					dp[st[i]][k]=min(dp[who[v]][k]+1, dp[st[i]][k]);
		dp[st[i]][2]=min(dp[st[i]][0]+dp[st[i]][1], dp[st[i]][2]);
		c.clear();
	}
	return ans+2*dp[who[s]][2];
}
# Verdict Execution time Memory Grader output
1 Correct 46 ms 70784 KB Output is correct
2 Correct 44 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 44 ms 70776 KB Output is correct
5 Correct 44 ms 70824 KB Output is correct
6 Correct 43 ms 70780 KB Output is correct
7 Correct 43 ms 70780 KB Output is correct
8 Correct 44 ms 70776 KB Output is correct
9 Correct 44 ms 70784 KB Output is correct
10 Correct 44 ms 70780 KB Output is correct
11 Correct 44 ms 70784 KB Output is correct
12 Correct 45 ms 70776 KB Output is correct
13 Correct 43 ms 70784 KB Output is correct
14 Correct 44 ms 70904 KB Output is correct
15 Correct 45 ms 70776 KB Output is correct
16 Correct 43 ms 70784 KB Output is correct
17 Correct 44 ms 70776 KB Output is correct
18 Correct 43 ms 70784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 70784 KB Output is correct
2 Correct 44 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 44 ms 70776 KB Output is correct
5 Correct 44 ms 70824 KB Output is correct
6 Correct 43 ms 70780 KB Output is correct
7 Correct 43 ms 70780 KB Output is correct
8 Correct 44 ms 70776 KB Output is correct
9 Correct 44 ms 70784 KB Output is correct
10 Correct 44 ms 70780 KB Output is correct
11 Correct 44 ms 70784 KB Output is correct
12 Correct 45 ms 70776 KB Output is correct
13 Correct 43 ms 70784 KB Output is correct
14 Correct 44 ms 70904 KB Output is correct
15 Correct 45 ms 70776 KB Output is correct
16 Correct 43 ms 70784 KB Output is correct
17 Correct 44 ms 70776 KB Output is correct
18 Correct 43 ms 70784 KB Output is correct
19 Correct 43 ms 70912 KB Output is correct
20 Correct 44 ms 70904 KB Output is correct
21 Correct 44 ms 70808 KB Output is correct
22 Correct 47 ms 71032 KB Output is correct
23 Correct 44 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 44 ms 70904 KB Output is correct
26 Correct 45 ms 70908 KB Output is correct
27 Correct 44 ms 70912 KB Output is correct
28 Correct 45 ms 70904 KB Output is correct
29 Correct 44 ms 70904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 70784 KB Output is correct
2 Correct 44 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 44 ms 70776 KB Output is correct
5 Correct 44 ms 70824 KB Output is correct
6 Correct 43 ms 70780 KB Output is correct
7 Correct 43 ms 70780 KB Output is correct
8 Correct 44 ms 70776 KB Output is correct
9 Correct 44 ms 70784 KB Output is correct
10 Correct 44 ms 70780 KB Output is correct
11 Correct 44 ms 70784 KB Output is correct
12 Correct 45 ms 70776 KB Output is correct
13 Correct 43 ms 70784 KB Output is correct
14 Correct 44 ms 70904 KB Output is correct
15 Correct 45 ms 70776 KB Output is correct
16 Correct 43 ms 70784 KB Output is correct
17 Correct 44 ms 70776 KB Output is correct
18 Correct 43 ms 70784 KB Output is correct
19 Correct 43 ms 70912 KB Output is correct
20 Correct 44 ms 70904 KB Output is correct
21 Correct 44 ms 70808 KB Output is correct
22 Correct 47 ms 71032 KB Output is correct
23 Correct 44 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 44 ms 70904 KB Output is correct
26 Correct 45 ms 70908 KB Output is correct
27 Correct 44 ms 70912 KB Output is correct
28 Correct 45 ms 70904 KB Output is correct
29 Correct 44 ms 70904 KB Output is correct
30 Correct 1147 ms 203108 KB Output is correct
31 Correct 1118 ms 203236 KB Output is correct
32 Correct 198 ms 80376 KB Output is correct
33 Correct 500 ms 163552 KB Output is correct
34 Correct 488 ms 163568 KB Output is correct
35 Correct 499 ms 163560 KB Output is correct
36 Correct 506 ms 163560 KB Output is correct
37 Correct 534 ms 162532 KB Output is correct
38 Correct 549 ms 155496 KB Output is correct
39 Correct 550 ms 155364 KB Output is correct
40 Correct 610 ms 155112 KB Output is correct
41 Correct 911 ms 179820 KB Output is correct
42 Correct 862 ms 173928 KB Output is correct
43 Correct 544 ms 202852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 71032 KB Output is correct
2 Correct 46 ms 70904 KB Output is correct
3 Correct 46 ms 70912 KB Output is correct
4 Correct 46 ms 70912 KB Output is correct
5 Correct 46 ms 70912 KB Output is correct
6 Correct 45 ms 70904 KB Output is correct
7 Correct 46 ms 70904 KB Output is correct
8 Correct 46 ms 70904 KB Output is correct
9 Correct 47 ms 70904 KB Output is correct
10 Correct 45 ms 70776 KB Output is correct
11 Correct 46 ms 70912 KB Output is correct
12 Correct 47 ms 70904 KB Output is correct
13 Correct 46 ms 70904 KB Output is correct
14 Correct 47 ms 70904 KB Output is correct
15 Correct 46 ms 70904 KB Output is correct
16 Correct 47 ms 70904 KB Output is correct
17 Correct 48 ms 71032 KB Output is correct
18 Correct 46 ms 70904 KB Output is correct
19 Correct 46 ms 70904 KB Output is correct
20 Correct 46 ms 70904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 70784 KB Output is correct
2 Correct 44 ms 70776 KB Output is correct
3 Correct 43 ms 70776 KB Output is correct
4 Correct 44 ms 70776 KB Output is correct
5 Correct 44 ms 70824 KB Output is correct
6 Correct 43 ms 70780 KB Output is correct
7 Correct 43 ms 70780 KB Output is correct
8 Correct 44 ms 70776 KB Output is correct
9 Correct 44 ms 70784 KB Output is correct
10 Correct 44 ms 70780 KB Output is correct
11 Correct 44 ms 70784 KB Output is correct
12 Correct 45 ms 70776 KB Output is correct
13 Correct 43 ms 70784 KB Output is correct
14 Correct 44 ms 70904 KB Output is correct
15 Correct 45 ms 70776 KB Output is correct
16 Correct 43 ms 70784 KB Output is correct
17 Correct 44 ms 70776 KB Output is correct
18 Correct 43 ms 70784 KB Output is correct
19 Correct 43 ms 70912 KB Output is correct
20 Correct 44 ms 70904 KB Output is correct
21 Correct 44 ms 70808 KB Output is correct
22 Correct 47 ms 71032 KB Output is correct
23 Correct 44 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 44 ms 70904 KB Output is correct
26 Correct 45 ms 70908 KB Output is correct
27 Correct 44 ms 70912 KB Output is correct
28 Correct 45 ms 70904 KB Output is correct
29 Correct 44 ms 70904 KB Output is correct
30 Correct 1147 ms 203108 KB Output is correct
31 Correct 1118 ms 203236 KB Output is correct
32 Correct 198 ms 80376 KB Output is correct
33 Correct 500 ms 163552 KB Output is correct
34 Correct 488 ms 163568 KB Output is correct
35 Correct 499 ms 163560 KB Output is correct
36 Correct 506 ms 163560 KB Output is correct
37 Correct 534 ms 162532 KB Output is correct
38 Correct 549 ms 155496 KB Output is correct
39 Correct 550 ms 155364 KB Output is correct
40 Correct 610 ms 155112 KB Output is correct
41 Correct 911 ms 179820 KB Output is correct
42 Correct 862 ms 173928 KB Output is correct
43 Correct 544 ms 202852 KB Output is correct
44 Correct 46 ms 71032 KB Output is correct
45 Correct 46 ms 70904 KB Output is correct
46 Correct 46 ms 70912 KB Output is correct
47 Correct 46 ms 70912 KB Output is correct
48 Correct 46 ms 70912 KB Output is correct
49 Correct 45 ms 70904 KB Output is correct
50 Correct 46 ms 70904 KB Output is correct
51 Correct 46 ms 70904 KB Output is correct
52 Correct 47 ms 70904 KB Output is correct
53 Correct 45 ms 70776 KB Output is correct
54 Correct 46 ms 70912 KB Output is correct
55 Correct 47 ms 70904 KB Output is correct
56 Correct 46 ms 70904 KB Output is correct
57 Correct 47 ms 70904 KB Output is correct
58 Correct 46 ms 70904 KB Output is correct
59 Correct 47 ms 70904 KB Output is correct
60 Correct 48 ms 71032 KB Output is correct
61 Correct 46 ms 70904 KB Output is correct
62 Correct 46 ms 70904 KB Output is correct
63 Correct 46 ms 70904 KB Output is correct
64 Correct 523 ms 201176 KB Output is correct
65 Correct 527 ms 201868 KB Output is correct
66 Correct 579 ms 184220 KB Output is correct
67 Correct 612 ms 182244 KB Output is correct
68 Correct 94 ms 84212 KB Output is correct
69 Correct 97 ms 82420 KB Output is correct
70 Correct 96 ms 84084 KB Output is correct
71 Correct 93 ms 83700 KB Output is correct
72 Correct 91 ms 83572 KB Output is correct
73 Correct 101 ms 82288 KB Output is correct
74 Correct 506 ms 196708 KB Output is correct
75 Correct 514 ms 191972 KB Output is correct
76 Correct 575 ms 209380 KB Output is correct
77 Correct 552 ms 208360 KB Output is correct
78 Correct 541 ms 182884 KB Output is correct
79 Correct 547 ms 191972 KB Output is correct
80 Correct 201 ms 85368 KB Output is correct
81 Correct 491 ms 189924 KB Output is correct
82 Correct 489 ms 189412 KB Output is correct
83 Correct 543 ms 192104 KB Output is correct
84 Correct 525 ms 191972 KB Output is correct
85 Correct 537 ms 192100 KB Output is correct
86 Correct 562 ms 191848 KB Output is correct
87 Correct 521 ms 191972 KB Output is correct
88 Correct 514 ms 192116 KB Output is correct
89 Correct 538 ms 191972 KB Output is correct
90 Correct 556 ms 191716 KB Output is correct
91 Correct 590 ms 185700 KB Output is correct
92 Correct 613 ms 181856 KB Output is correct