Submission #806346

# Submission time Handle Problem Language Result Execution time Memory
806346 2023-08-04T06:09:47 Z fatemetmhr Ancient Books (IOI17_books) C++17
50 / 100
119 ms 24652 KB
//  ~ Be Name Khoda ~  //

#include "books.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back

#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  1e6   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 7;
const ll  inf   =  1e18;

int cmp[maxn5], lef[maxn5], rig[maxn5], num = 0;
bool mark[maxn5];
vector <int> per;

void expa(int &l, int &r){
	int lq = l, rq = r;
	while(true){
		lq = min({lq, lef[cmp[l]], lef[cmp[r]]});
		rq = max({rq, rig[cmp[l]], rig[cmp[r]]});
		//cout << "haaaa " << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
		if(l > lq){
			l--;
			continue;
		}
		if(r < rq){
			r++;
			continue;
		}
		break;
	}
}

int solve(int lq, int rq, int s){
	int l = s, r = s;
	expa(l, r);
	int ans = 0;
	//cout << l << ' ' << r << ' ' << lq << ' ' << rq << endl;
	while(l > lq || r < rq){
		int c1 = 0, c2 = 0;
		int l1 = l, r1 = r, l2 = l, r2 = r;
		while(r1 <= r && l1 > lq){
			l1--;
			c1++;
			expa(l1, r1);
		}
		while(l2 >= l && r2 < rq){
			r2++;
			//cout << r2 << ' ' << cmp[r2] << ' ' << rig[cmp[r2]] << endl;
			c2++;
			expa(l2, r2);
			//cout << "hoy " << l2 << ' ' << r2 << ' ' << rig[cmp[r2]] << ' ' << cmp[r2] << endl;
		}
		ans += min(c1, c2) * 2;
		l = min(l1, l2);
		r = max(r1, r2);
		if(l2 != lq || r1 != rq)
			ans += max(c1, c2) * 2;
		//cout << l << ' ' << r << ' ' << ans << ' ' << c1 << ' ' << c2 << endl;
		//cout << l2 << ' ' << r2 << endl;
	}
	return ans;
}

void dfs(int v){
	mark[v] = true;
	cmp[v] = num;
	lef[num] = min(lef[num], v);
	rig[num] = max(rig[num], v);
	if(!mark[per[v]])
		dfs(per[v]);
}


long long minimum_walk(std::vector<int> PER, int s) {
	per = PER;
	int n = per.size();
	int r = s, l = s;
	ll ans = 0;
	for(int i = 0; i < n; i++){
		if(!mark[i]){
			lef[num] = rig[num] = i;
			dfs(i);
			num++;
		}
		//cout << i << ' ' << per[i] << ' ' << cmp[i] << endl;
		ans += abs(per[i] - i);
		if(i != per[i]){
			l = min(l, i);
			r = max(r, i);
		}
	}
	if(l == r)
		return 0;
	//cout << "ha? " << ans << endl;
	//cout << l << ' ' << r << ' ' << s << ' ' << cmp[l] << ' ' << cmp[r] << ' ' << num << ' ' << ans << endl;
	return solve(l, r, s) + ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 107 ms 16948 KB Output is correct
31 Correct 118 ms 16940 KB Output is correct
32 Correct 89 ms 24652 KB Output is correct
33 Correct 92 ms 22260 KB Output is correct
34 Correct 104 ms 22256 KB Output is correct
35 Correct 119 ms 21216 KB Output is correct
36 Correct 90 ms 18932 KB Output is correct
37 Correct 84 ms 17104 KB Output is correct
38 Correct 81 ms 16884 KB Output is correct
39 Correct 84 ms 17024 KB Output is correct
40 Correct 86 ms 16884 KB Output is correct
41 Correct 94 ms 16880 KB Output is correct
42 Correct 92 ms 16892 KB Output is correct
43 Correct 91 ms 22656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22154'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 107 ms 16948 KB Output is correct
31 Correct 118 ms 16940 KB Output is correct
32 Correct 89 ms 24652 KB Output is correct
33 Correct 92 ms 22260 KB Output is correct
34 Correct 104 ms 22256 KB Output is correct
35 Correct 119 ms 21216 KB Output is correct
36 Correct 90 ms 18932 KB Output is correct
37 Correct 84 ms 17104 KB Output is correct
38 Correct 81 ms 16884 KB Output is correct
39 Correct 84 ms 17024 KB Output is correct
40 Correct 86 ms 16884 KB Output is correct
41 Correct 94 ms 16880 KB Output is correct
42 Correct 92 ms 16892 KB Output is correct
43 Correct 91 ms 22656 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 308 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22154'
50 Halted 0 ms 0 KB -