Submission #806292

# Submission time Handle Problem Language Result Execution time Memory
806292 2023-08-04T05:42:58 Z fatemetmhr Ancient Books (IOI17_books) C++17
0 / 100
1 ms 340 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(lef[cmp[l]], lef[cmp[r]]);
		rq = max(rig[cmp[l]], rig[cmp[r]]);
		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++;
			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(l == lq && r == rq)
			ans += max(c1, c2) * 2;
		//cout << l << ' ' << r << ' ' << ans << ' ' << c1 << ' ' << c2 << 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;
	return solve(l, r, s) + ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4186'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -