Submission #288068

#TimeUsernameProblemLanguageResultExecution timeMemory
288068user202729Ancient Books (IOI17_books)C++17
22 / 100
176 ms8568 KiB
// moreflags=grader.cpp
// 11
// :(
// Definitely cheating.
#include "books.h"
#include<algorithm>
#include<numeric>


long long minimum_walk(std::vector<int> p, int s) {
	if(p.size()>1000) return -1;

	int needLeft=0; while(needLeft<(int)p.size() and p[needLeft]==needLeft) ++needLeft;
	if(needLeft==(int)p.size()) return 0;
	int needRight=(int)p.size()-1; while(p[needRight]==needRight) --needRight;

	int64_t result{};
	for(int index=0; index<(int)p.size(); ++index){
		if(index<p[index]){
			result+=p[index]-index;
		}
	}

	int left=s, right=s;

	while(left>needLeft or right<needRight){
		for(int i=left; i<=right; ++i){
			if(p[i]<left){ left=p[i]; goto continue_outer; }
			if(p[i]>right){ right=p[i]; goto continue_outer; }
		}
		for(int i=1;; ++i){
			if(left-i>=0 and p[left-i]!=left-i){
				result+=i;
				left-=i;
				break;
			}else if(right+i<(int)p.size() and p[right+i]!=right+i){
				result+=i;
				right+=i;
				break;
			}
		}
continue_outer:;
	}

	return result*2;
}
#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...