제출 #1045288

#제출 시각아이디문제언어결과실행 시간메모리
1045288beaconmc고대 책들 (IOI17_books)C++14
0 / 100
0 ms348 KiB
#include "books.h"

#include <bits/stdc++.h>

 
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
 
using namespace std;


long long minimum_walk(vector<int> p, int s) {
	ll ans = 0;
	ll prev = s;

	int cur = -1;
	ll lo=0, hi=p.size()-1;
	while (lo < hi){

		while (p[lo] == lo && lo < hi){
			lo++;
		}
		while (p[hi] == hi && lo < hi){
			hi--;
		}
		if (!(lo<hi)) break;

		ans += abs(prev-lo);
		prev = lo;

		
		FOR(i,lo,hi+1){
			if (p[i] > cur){
				swap(p[i], cur);
			}else if (cur == i){
				swap(p[i],cur);
			}
		}

		ans += (hi-lo);
		FORNEG(i, hi, lo-1){
			if (p[i] < cur){
				swap(p[i], cur);
			}else if (cur == i){
				swap(p[i],cur);
			}
		}
		ans += (hi-lo);
	}
	ans += abs(0-prev);

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