Submission #54217

# Submission time Handle Problem Language Result Execution time Memory
54217 2018-07-02T20:15:21 Z radoslav11 Ancient Books (IOI17_books) C++14
70 / 100
2000 ms 119240 KB
#include <bits/stdc++.h>
#include "books.h"
//#include "Lgrader.cpp"

using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);

int n;
int p[MAXN];
bool used[MAXN];

pair<int, int> dfs(int u)
{
	used[u] = true;
	pair<int, int> ret = {u, u};

	if(!used[p[u]])
	{
		pair<int, int> oth = dfs(p[u]);
		chkmax(ret.second, oth.second);
		chkmin(ret.first, oth.first);
	}

	return ret;
}

/*
int L, R;
int mn[1 << 10][1 << 10];
int mx[1 << 10][1 << 10];
int dp[1 << 10][1 << 10];

int Min(int l, int r)
{
	if(l == r) return p[l];
	int &memo = mn[l][r];
	if(memo != -1) return memo;
	return memo = min(Min(l + 1, r), Min(l, r - 1));
}

int Max(int l, int r)
{
	if(l == r) return p[l];
	int &memo = mx[l][r];
	if(memo != -1) return memo;
	return memo = max(Max(l + 1, r), Max(l, r - 1));
}

int rec(int l, int r)
{
	if(l == L && r == R)
		return 0;

	int &memo = dp[l][r];
	if(memo != -1) return memo;

	int nl = Min(l, r);
	if(nl < l) return memo = rec(nl, r);
	
	int nr = Max(l, r);
	if(r < nr) return memo = rec(l, nr);

	memo = (int)1e9;
	if(L < l) chkmin(memo, rec(l - 1, r) + 2);
	if(r < R) chkmin(memo, rec(l, r + 1) + 2);

	return memo;
}*/

int mn[MAXN];
int mx[MAXN];
int lg2[MAXN];

int dist[MAXN];
bool in_queue[MAXN];

long long minimum_walk(std::vector<int> p, int s) 
{
	n = p.size();
	for(int i = 0; i < n; i++)
		::p[i] = p[i];

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

	vector<pair<int, int> > li;
	for(int i = 0; i < n; i++)
		if(i != p[i] && !used[i])
			li.push_back(dfs(i));				
	li.push_back({s, s});

	sort(li.begin(), li.end());
	
	vector<pair<int, int> > tmp;
	for(int i = 0; i < (int)li.size(); i++)
		if(tmp.empty() || tmp.back().second < li[i].first) tmp.push_back(li[i]);
		else chkmax(tmp[tmp.size() - 1].second, li[i].second);

	li = tmp;

	int last = li[0].first;
	for(auto it: li)
	{
		answer += 2 * (it.first - last);
		last = it.second;
	}

	if(s != 0)
	{
		int l, r;
		for(auto it: li)
			if(it.first <= s && s <= it.second)
				l = it.first, r = it.second;

		//memset(mn, -1, sizeof(mn));
		//memset(mx, -1, sizeof(mx));
		//memset(dp, -1, sizeof(dp));
		//answer += rec(s, s);

		mn[s] = mx[s] = p[s];		
		for(int i = s + 1; i <= r; i++) mn[i] = min(mn[i - 1], p[i]);
		for(int i = s + 1; i <= r; i++) mx[i] = max(mx[i - 1], p[i]);
		for(int i = s - 1; i >= l; i--) mn[i] = min(mn[i + 1], p[i]);
		for(int i = s - 1; i >= l; i--) mx[i] = max(mx[i + 1], p[i]);
	
		for(int i = l; i <= r; i++) dist[i] = (int)1e9;

		queue<int> Q;
		Q.push(s);
		dist[s] = 0;
		in_queue[s] = 1;

		while(!Q.empty())
		{
			int u = Q.front();
			Q.pop();
			in_queue[u] = 0;

			if(chkmin(dist[mn[u]], dist[u]))
			{
				if(!in_queue[mn[u]]) Q.push(mn[u]);
				in_queue[mn[u]] = 1;
			}

			if(chkmin(dist[mx[u]], dist[u]))
			{
				if(!in_queue[mx[u]]) Q.push(mx[u]);
				in_queue[mx[u]] = 1;
			}
			
			if(u < r && chkmin(dist[u + 1], 2 + dist[u]))
			{
				if(!in_queue[u + 1]) Q.push(u + 1);
				in_queue[u + 1] = 1;
			}
	
			if(u > l && chkmin(dist[u - 1], 2 + dist[u]))
			{
				if(!in_queue[u - 1]) Q.push(u - 1);
				in_queue[u - 1] = 1;
			}
		}

		answer += min(dist[l], dist[r]);
	}

	return answer;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:113:10: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int l, r;
          ^
books.cpp:113:7: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int l, r;
       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
9 Correct 2 ms 688 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 2 ms 760 KB Output is correct
14 Correct 2 ms 760 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 760 KB Output is correct
18 Correct 2 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
9 Correct 2 ms 688 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 2 ms 760 KB Output is correct
14 Correct 2 ms 760 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 760 KB Output is correct
18 Correct 2 ms 760 KB Output is correct
19 Correct 3 ms 760 KB Output is correct
20 Correct 2 ms 760 KB Output is correct
21 Correct 2 ms 760 KB Output is correct
22 Correct 2 ms 760 KB Output is correct
23 Correct 2 ms 760 KB Output is correct
24 Correct 2 ms 760 KB Output is correct
25 Correct 2 ms 760 KB Output is correct
26 Correct 2 ms 760 KB Output is correct
27 Correct 2 ms 760 KB Output is correct
28 Correct 2 ms 760 KB Output is correct
29 Correct 2 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
9 Correct 2 ms 688 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 2 ms 760 KB Output is correct
14 Correct 2 ms 760 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 760 KB Output is correct
18 Correct 2 ms 760 KB Output is correct
19 Correct 3 ms 760 KB Output is correct
20 Correct 2 ms 760 KB Output is correct
21 Correct 2 ms 760 KB Output is correct
22 Correct 2 ms 760 KB Output is correct
23 Correct 2 ms 760 KB Output is correct
24 Correct 2 ms 760 KB Output is correct
25 Correct 2 ms 760 KB Output is correct
26 Correct 2 ms 760 KB Output is correct
27 Correct 2 ms 760 KB Output is correct
28 Correct 2 ms 760 KB Output is correct
29 Correct 2 ms 760 KB Output is correct
30 Correct 215 ms 25552 KB Output is correct
31 Correct 211 ms 25552 KB Output is correct
32 Correct 134 ms 25552 KB Output is correct
33 Correct 167 ms 25552 KB Output is correct
34 Correct 169 ms 25552 KB Output is correct
35 Correct 173 ms 25552 KB Output is correct
36 Correct 181 ms 25552 KB Output is correct
37 Correct 152 ms 25552 KB Output is correct
38 Correct 156 ms 25552 KB Output is correct
39 Correct 156 ms 25552 KB Output is correct
40 Correct 213 ms 25552 KB Output is correct
41 Correct 195 ms 25552 KB Output is correct
42 Correct 187 ms 25552 KB Output is correct
43 Correct 177 ms 25552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 25552 KB Output is correct
2 Correct 3 ms 25552 KB Output is correct
3 Correct 3 ms 25552 KB Output is correct
4 Correct 2 ms 25552 KB Output is correct
5 Correct 2 ms 25552 KB Output is correct
6 Correct 2 ms 25552 KB Output is correct
7 Correct 2 ms 25552 KB Output is correct
8 Correct 2 ms 25552 KB Output is correct
9 Correct 3 ms 25552 KB Output is correct
10 Correct 2 ms 25552 KB Output is correct
11 Correct 2 ms 25552 KB Output is correct
12 Correct 2 ms 25552 KB Output is correct
13 Correct 2 ms 25552 KB Output is correct
14 Correct 2 ms 25552 KB Output is correct
15 Correct 3 ms 25552 KB Output is correct
16 Correct 2 ms 25552 KB Output is correct
17 Correct 3 ms 25552 KB Output is correct
18 Correct 2 ms 25552 KB Output is correct
19 Correct 2 ms 25552 KB Output is correct
20 Correct 3 ms 25552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
8 Correct 2 ms 688 KB Output is correct
9 Correct 2 ms 688 KB Output is correct
10 Correct 4 ms 688 KB Output is correct
11 Correct 2 ms 760 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
13 Correct 2 ms 760 KB Output is correct
14 Correct 2 ms 760 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 2 ms 760 KB Output is correct
17 Correct 2 ms 760 KB Output is correct
18 Correct 2 ms 760 KB Output is correct
19 Correct 3 ms 760 KB Output is correct
20 Correct 2 ms 760 KB Output is correct
21 Correct 2 ms 760 KB Output is correct
22 Correct 2 ms 760 KB Output is correct
23 Correct 2 ms 760 KB Output is correct
24 Correct 2 ms 760 KB Output is correct
25 Correct 2 ms 760 KB Output is correct
26 Correct 2 ms 760 KB Output is correct
27 Correct 2 ms 760 KB Output is correct
28 Correct 2 ms 760 KB Output is correct
29 Correct 2 ms 760 KB Output is correct
30 Correct 215 ms 25552 KB Output is correct
31 Correct 211 ms 25552 KB Output is correct
32 Correct 134 ms 25552 KB Output is correct
33 Correct 167 ms 25552 KB Output is correct
34 Correct 169 ms 25552 KB Output is correct
35 Correct 173 ms 25552 KB Output is correct
36 Correct 181 ms 25552 KB Output is correct
37 Correct 152 ms 25552 KB Output is correct
38 Correct 156 ms 25552 KB Output is correct
39 Correct 156 ms 25552 KB Output is correct
40 Correct 213 ms 25552 KB Output is correct
41 Correct 195 ms 25552 KB Output is correct
42 Correct 187 ms 25552 KB Output is correct
43 Correct 177 ms 25552 KB Output is correct
44 Correct 2 ms 25552 KB Output is correct
45 Correct 3 ms 25552 KB Output is correct
46 Correct 3 ms 25552 KB Output is correct
47 Correct 2 ms 25552 KB Output is correct
48 Correct 2 ms 25552 KB Output is correct
49 Correct 2 ms 25552 KB Output is correct
50 Correct 2 ms 25552 KB Output is correct
51 Correct 2 ms 25552 KB Output is correct
52 Correct 3 ms 25552 KB Output is correct
53 Correct 2 ms 25552 KB Output is correct
54 Correct 2 ms 25552 KB Output is correct
55 Correct 2 ms 25552 KB Output is correct
56 Correct 2 ms 25552 KB Output is correct
57 Correct 2 ms 25552 KB Output is correct
58 Correct 3 ms 25552 KB Output is correct
59 Correct 2 ms 25552 KB Output is correct
60 Correct 3 ms 25552 KB Output is correct
61 Correct 2 ms 25552 KB Output is correct
62 Correct 2 ms 25552 KB Output is correct
63 Correct 3 ms 25552 KB Output is correct
64 Correct 225 ms 26976 KB Output is correct
65 Correct 179 ms 33652 KB Output is correct
66 Correct 188 ms 39920 KB Output is correct
67 Correct 193 ms 46684 KB Output is correct
68 Correct 21 ms 46684 KB Output is correct
69 Correct 35 ms 46684 KB Output is correct
70 Correct 35 ms 46684 KB Output is correct
71 Correct 37 ms 46684 KB Output is correct
72 Correct 18 ms 46684 KB Output is correct
73 Correct 35 ms 46684 KB Output is correct
74 Correct 162 ms 48476 KB Output is correct
75 Correct 163 ms 55168 KB Output is correct
76 Correct 208 ms 72308 KB Output is correct
77 Correct 201 ms 78916 KB Output is correct
78 Correct 198 ms 83980 KB Output is correct
79 Correct 191 ms 90788 KB Output is correct
80 Correct 137 ms 90788 KB Output is correct
81 Correct 227 ms 107952 KB Output is correct
82 Correct 183 ms 114544 KB Output is correct
83 Execution timed out 2047 ms 119240 KB Time limit exceeded
84 Halted 0 ms 0 KB -