답안 #294932

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
294932 2020-09-09T10:53:21 Z amoo_safar 고대 책들 (IOI17_books) C++17
70 / 100
2000 ms 32604 KB
#include "books.h"

#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int N = 1e6 + 10;

int mk[N], mk2[N];
int L[N], R[N];
//int mn[N][N], mx[N][N];
//ll dp[N][N];

ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	ll ans = 0;
	ll near = n;
	for(int i = 0; i < n; i++){
		if(p[i] != i){
			ans += abs(p[i] - i);
		} else {
			//mk[i] = 1;
			R[i] = i;
			L[i] = i;
		}
	}
	cerr << "# " << ans << '\n';
	mk[s] = 0;
	int st = 0, en = n - 1;
	while(p[st] == st && st < s){ mk[st] = 1; st ++; }
	while(p[en] == en && en > s){ mk[en] = 1; en --; }

	int com = 0;
	vector<pii> seg;
	for(int i = st; i < en; i++){
		if(mk[i]) continue;
		com ++;
		vector<int> vis;
		int nw = i;
		while(!mk[nw]){
			mk[nw] = 1;
			vis.pb(nw);
			nw = p[nw];
		}
		sort(all(vis));
		seg.pb({vis[0], vis.back()});
		for(auto x : vis) L[x] = vis[0];
		for(auto x : vis) R[x] = vis.back();
	}
	if(com == 0) return 0;
	sort(all(seg));
	int mxr = st;
	for(int i = 0; i < (int)seg.size(); i++){
		if(mxr < seg[i].F)
			mk2[mxr] = 1, ans += 2;
		mxr = max(mxr, seg[i].S);
	}
	mk2[mxr] = 1;
	/*
	for(int i = 0; i < n; i++) mn[i][i] = L[i];
	for(int i = 0; i < n; i++) mx[i][i] = R[i];
	for(int ln = 1; ln < n; ln ++){
		for(int i = 0; i + ln < n; i++){
			int j = i + ln;
			mn[i][j] = min(mn[i][j - 1], mn[i + 1][j]);
			mx[i][j] = max(mx[i][j - 1], mx[i + 1][j]);
		}
	}
	*/
	//if(ans == 0) return 0;

	int nwl = s;
	int nwr = s;
	int mxl = L[s];
	mxr = R[s];
	cerr << ans << '\n';
	while(!mk2[nwr]){
		
		mxl = L[nwl];
		for(int i = nwl; i <= nwr; i ++) mxl = min(mxl, L[i]);
		mxr = R[nwl];
		for(int i = nwl; i <= nwr; i ++) mxr = max(mxr, R[i]);
		//cerr << "! " << nwl << ' ' << nwr << ' ' << l2 << ' ' << r2 << '\n';
		
		int l2 = mxl;
		int r2 = mxr;
		
		if(pii(l2, r2) == pii(nwl, nwr) && !mk2[r2]){
			ans += 2;
			l2 --;
			r2 ++;
		}
		while(nwl > l2){nwl --; mxl = min(mxl, L[nwl]);}
		while(nwr < r2){nwr ++; mxr = max(mxr, R[nwr]);}
		//nwl = l2;
		//nwr = r2;
	}

	return ans;
}
/*
4 1
0 3 1 2

*/

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:25:5: warning: unused variable 'near' [-Wunused-variable]
   25 |  ll near = n;
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 375 ms 27160 KB Output is correct
31 Correct 368 ms 25244 KB Output is correct
32 Correct 165 ms 23160 KB Output is correct
33 Correct 256 ms 32604 KB Output is correct
34 Correct 274 ms 32596 KB Output is correct
35 Correct 256 ms 31380 KB Output is correct
36 Correct 235 ms 29160 KB Output is correct
37 Correct 253 ms 27372 KB Output is correct
38 Correct 244 ms 25656 KB Output is correct
39 Correct 246 ms 25400 KB Output is correct
40 Correct 261 ms 23732 KB Output is correct
41 Correct 311 ms 24004 KB Output is correct
42 Correct 300 ms 24164 KB Output is correct
43 Correct 274 ms 31004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 404 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 375 ms 27160 KB Output is correct
31 Correct 368 ms 25244 KB Output is correct
32 Correct 165 ms 23160 KB Output is correct
33 Correct 256 ms 32604 KB Output is correct
34 Correct 274 ms 32596 KB Output is correct
35 Correct 256 ms 31380 KB Output is correct
36 Correct 235 ms 29160 KB Output is correct
37 Correct 253 ms 27372 KB Output is correct
38 Correct 244 ms 25656 KB Output is correct
39 Correct 246 ms 25400 KB Output is correct
40 Correct 261 ms 23732 KB Output is correct
41 Correct 311 ms 24004 KB Output is correct
42 Correct 300 ms 24164 KB Output is correct
43 Correct 274 ms 31004 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 1 ms 384 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 1 ms 384 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 1 ms 384 KB Output is correct
62 Correct 1 ms 404 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 284 ms 30492 KB Output is correct
65 Execution timed out 2052 ms 30180 KB Time limit exceeded
66 Halted 0 ms 0 KB -