제출 #299740

#제출 시각아이디문제언어결과실행 시간메모리
299740errorgornAncient Books (IOI17_books)C++14
50 / 100
202 ms26744 KiB
#include "books.h"

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

#define ll long long
#define ii pair<int,int>
#define fi first
#define se second

#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

int n,k;
vector<int> arr;

int fwd[1000005];
int bwd[1000005];

long long minimum_walk(std::vector<int> p, int s) {
	n=sz(p),k=s;
	arr=p;
	
	rep(x,0,n){
		if (x<arr[x]) fwd[x]++,fwd[arr[x]]--;
		else bwd[arr[x]]++,bwd[x]--;
	}
	
	rep(x,0,n) fwd[x+1]+=fwd[x],bwd[x+1]+=bwd[x];
	
	ll ans=0;
	rep(x,n,0){
		int val=max(fwd[x],bwd[x]);
		
		if (val==0){
			if (ans) ans++;
		}
		else{
			ans+=val;
		}
	}
	
	return ans*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...