Submission #132680

#TimeUsernameProblemLanguageResultExecution timeMemory
132680MoNsTeR_CuBe고대 책들 (IOI17_books)C++17
50 / 100
160 ms8184 KiB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

long long minimum_walk(std::vector<int> p, int s) {
	#define int long long 
	int l = 0, r = p.size()-1;
	int maxi = -1;
	int ans = -2;
	int n = p.size();
	for(int i = 0; i < (int)p.size(); i++){
		l = i;
		if(p[i] != i){
			break;
		}
	}
	
	for(int i = (int)p.size()-1; i >= 0; i--){
		r = i;
		if(p[i] != i){
			break;
		}
	}
	
	if(l >= r) return 0;
	
	if(s == 0){
		for(int i = l; i <= r; i++){
			ans += abs(p[i]-i);
			maxi = max(maxi, (int)p[i]);
			if(i >= maxi){
				ans += 2;
			}
		}
		
		return ans + 2*l;
	}
	
	vector< bool > visited(n, false);
	
	ans = 0;
	
	int REP = numeric_limits<int>::max();
	
	int curr = s;
	while(curr >= 0){
		if(p[curr] != curr){
			REP = min(REP, s-curr);
			break;
		}
		curr--;
	}
	curr = s;
	while(curr < n){
		if(p[curr] != curr){
			REP = min(REP, curr-s);
			break;
		}
		curr++;
	}
	
	for(int i = 0; i < n; i++){
		ans += abs(i-p[i]);
		for(int j = i; j < p[i]; j++){
			visited[j] = true;
		}
	}
	
	for(int i = min(l,(int)s); i <= max((int)s,r); i++){
		ans += (!visited[i])*2;
	}
	return ans + 2*REP;
	#undef int
}
 
#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...