제출 #105243

#제출 시각아이디문제언어결과실행 시간메모리
105243bert30702고대 책들 (IOI17_books)C++17
50 / 100
279 ms20220 KiB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const int MX = 1e6 + 100;
int mn[MX], mx[MX];
long long minimum_walk(vector<int> p, int s) {
	int n = p.size(), t = 0;
	for(int i = 0; i < n; i ++) mn[i] = i;
	while(p[n - 1] == n - 1 and n - 1 > s) n --;
	while(p[t    ] == t     and t     < s) t ++;
	long long sum = 0;
	auto update = [&] (int s, int p) {
		mx[s] = max(mx[s], mx[p]);
		mn[s] = min(mn[s], mn[p]);
	};
	for(int i = t; i < n; i ++) {
		mx[p[i]] = max(mx[p[i]], i);
		mn[p[i]] = min(mn[p[i]], i);
		mx[i]    = max(mx[i], p[i]);
		mn[i]    = min(mn[i], p[i]);
		sum += abs(i - p[i]);
	}
	queue<int> l, r;
	for(int i = s - 1; i >= t; i --) l.push(i);
	for(int i = s + 1; i <  n; i ++) r.push(i);
	srand(time(0));
	while(1) {
			 if(l.size() and l.front() >= mn[s]) update(s, l.front()), l.pop();
		else if(r.size() and r.front() <= mx[s]) update(s, r.front()), r.pop();
		else if(l.size() and r.size() and rand() % 2) {
			sum += 2;
			update(s, r.front()), r.pop();
		}
		else if(l.size()) sum += 2, update(s, l.front()), l.pop();
		else if(r.size()) sum += 2, update(s, r.front()), r.pop();
		else break;
	}
	return sum;
}
//main () {
//	cout << minimum_walk({2, 3, 0, 1}, 0);
//}
#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...