Submission #61781

#TimeUsernameProblemLanguageResultExecution timeMemory
61781zadrgaAncient Books (IOI17_books)C++14
50 / 100
238 ms106952 KiB
#include "books.h"
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 1001111

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

int pre[maxn];

long long minimum_walk(vector<int> p, int s){
	int n = p.size();
	ll ans = 0;
	for(int i = 0; i < n; i++){
		ans += abs(p[i] - i);
		int l = min(i, p[i]);
		int d = max(i, p[i]);
		pre[l]++;
		pre[d]--;
	}

	for(int i = 1; i < n; i++)
		pre[i] += pre[i - 1];

	int last = -1;
	for(int i = n - 1; i >= 0; i--){
		if(pre[i] > 0){
			last = i;
			break;
		}
	}

	for(int i = 0; i <= last; i++){
		if(pre[i] == 0)
			ans += 2;
	}

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