답안 #618034

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
618034 2022-08-01T19:48:04 Z StrawHatWess 고대 책들 (IOI17_books) C++17
0 / 100
1 ms 468 KB
#include "books.h"

#include <bits/stdc++.h>
using namespace std; 

typedef long long ll; 
typedef vector<int>vi;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()

#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)

void dbgv(vi vec){for(int x: vec) cout << x << ' '; cout << endl; }

//---------------------------

int N; 
vi a,p; 

int check(){
	FOR(i,0,N) if(a[i]==-1 || p[a[i]]!=i) return 0; 
	return 1;  	
}

int dist(int i, int j){
	return abs(i-j); 
}

ll minimum_walk(vi p, int s) {
	::p=p; 
	N=sz(p); 
	a.resize(N); 
	iota(all(a),0); 

		
	int l=0, r=N-1, on=-1;
	ll ans=0;  
	while(!check()){
		while(p[a[l]]==l) l++, ans++; 
		while(p[a[r]]==r) r--; 

		//if(l>r) break; 

		FOR(i,l,r+1){
			if(p[a[i]]>i){
				if(on==-1 || p[a[i]]>p[on]) swap(a[i], on); 
			}
			else if(on!=-1 && i==p[on]){
				assert(i==r); 
				swap(a[i], on); 
			}
		}

		ROF(i,l,r+1){
			if(a[i]==-1){
				assert(i==l); 
				swap(a[i], on); 
				break;
			}

			if(p[a[i]]<i){
				if(p[a[i]]<p[on]) swap(a[i], on); 
			}
		}


		ans+=(r-l)*2; 
	}	
	ans+=l; 
	
	return ans; 
}


/*

4 0
0 2 3 1


*/

/*

3 0
2 0 1

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 304 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 304 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 304 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 304 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -