Submission #69341

#TimeUsernameProblemLanguageResultExecution timeMemory
69341BruteforcemanAncient Books (IOI17_books)C++11
0 / 100
41 ms5400 KiB
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
int col[1000010];
bool vis[1000010];

long long minimum_walk(std::vector<int> p, int s) {
	long long ans = 0;
	int n = p.size();
	for(int i = 0; i < n-1; i++) {
		int go_left = 0;
		int go_right = 0;
		for(int j = 0; j <= i; j++) {
			if(p[j] > i) {
				++go_left;
			}
		}
		for(int j = i + 1; j < n; j++) {
			if(p[j] < i+1) {
				++go_right;
			}
		}
		assert(go_left == go_right);
		ans += go_left + go_right;
	}
	memset(col, -1, sizeof col);
	int id = 0;
	for(int i = 0; i < n; i++) {
		if(col[i] != -1 || p[i] == i) continue;
		int cur = i;
		++id;
		while(col[cur] == -1) {
			col[cur] = id;
			cur = p[cur];
		}
	}
	int opt = 1e9;
	for(int i = s; i < n; i++) {
		memset(vis, false, sizeof vis);
		int cnt = 0;
		int add = abs(s - i);
		for(int j = s; j <= i; j++) {
			if(col[j] == -1)  continue;
			if(vis[col[j]] == false) ++cnt;
			vis[col[j]] = true;
		} 
		if(cnt == id) {
			opt = min(opt, 2 * add);
			continue;
		}
		for(int j = s-1; j >= 0; j--) {
			if(col[j] == -1) continue;
			if(vis[col[j]] == false) ++cnt;
			vis[col[j]] = true;
			if(cnt == id) {
				add += abs(s - j);
				break;
			}
		}
		if(cnt == id) {
			opt = min(opt, 2 * add);
		}
	}
	ans += opt;
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...