Submission #364618

#TimeUsernameProblemLanguageResultExecution timeMemory
364618rqiAncient Books (IOI17_books)C++14
50 / 100
181 ms22892 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef long long ll;

#define sz(x) (int)(x).size()

long long minimum_walk(vi p, int s) {
	bool all_good = 1;
	for(int i = 0; i < sz(p); i++){
		if(p[i] != i) all_good = 0;
	}
	if(all_good) return 0;

	int n = sz(p);
	vi cover = vi(n, 0);
	vi dirover = vi(n, 0); //right = +1
	ll ans = 0;
	for(int i = 0; i < n; i++){
		cover[min(i, p[i])]++;
		cover[max(i, p[i])]--;
		dirover[i]++;
		dirover[p[i]]--;
		ans+=abs(p[i]-i);
	}

	//cout << ans << "\n";

	for(int i = 1; i < n; i++){
		cover[i]+=cover[i-1];
		dirover[i]+=dirover[i-1];
	}

	

	for(int i = 0; i < n; i++){
		ans+=abs(dirover[i]);
	}

	int L = 0;
	int R = n-2;
	for(int i = 0; i <= s-1; i++){
		if(p[i] == i){
			L = i+1;
		}
		else break;
	}
	for(int i = n-2; i >= s; i--){
		if(p[i+1] == i+1){
			R = i-1;
		}
		else break;
	}

	for(int i = L; i <= R; i++){
		if(cover[i] == 0){
			ans+=2;
		}
	}

	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...