제출 #430375

#제출 시각아이디문제언어결과실행 시간메모리
430375AmineWeslatiAncient Books (IOI17_books)C++14
0 / 100
2082 ms316 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std; 

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


typedef pair<int,int>pi;
#define fi first
#define se second
typedef vector<pi>vpi;
#define eb emplace_back

#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--)

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

const int MX=1e6+5;
int N; 
vi a;

ll minimum_walk(vi p, int s){
	N=sz(p);
	a.resize(N);
	FOR(i,0,N) a[i]=i;

	ll ans=0,cur=s;
	while(1){
		vpi vec;

		FOR(i,0,N) if(p[a[i]]>i) vec.eb(i,a[i]);
		if(!sz(vec)) break;

		int rgt=p[vec.back().fi];
		vec.eb(rgt,a[rgt]);


		/*for(auto x: vec) cout << x.fi << ' ' << x.se << endl;
		break;*/

		int emp=vec[0].fi;

		vpi vecc;
		vecc.eb(emp,a[emp]);

		FOR(i,emp+1,N){
			if(p[a[i]]<i) vecc.eb(i,a[i]);
		}

		/*for(auto x: vecc) cout << x.fi << ' ' << x.se << endl;
		break;*/
		

		ROF(i,0,sz(vec)-1){
			a[vec[i+1].fi]=vec[i].se;
		}
		FOR(i,1,sz(vecc)){
			a[vecc[i-1].fi]=vecc[i].se;
		}

		/*for(auto x: a) cout << x << ' ';
		cout << endl;*/

		ans+=abs(cur-rgt);
		ans+=abs(rgt-emp);

		cur=emp;
	}
	ans+=abs(cur-s);
	
	
	return ans; 
}

/*

4 0
0 2 3 1

*/
#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...