Submission #328329

#TimeUsernameProblemLanguageResultExecution timeMemory
328329arnold518Ancient Books (IOI17_books)C++14
50 / 100
266 ms43080 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e6;

int N;
int A[MAXN+10], B[MAXN+10], S;
ll ans;

ll minimum_walk(vector<int> _P, int _S)
{
	N=_P.size(); S=_S+1;
	for(int i=1; i<=N; i++) A[i]=_P[i-1]+1;
	for(int i=1; i<=N; i++) B[A[i]]=i;

	for(int i=1; i<=N; i++) ans+=abs(i-A[i]);

	vector<pii> V;
	for(int i=1; i<=N; i++)
	{
		int l=min(i, A[i]), r=max(i, A[i]);
		V.push_back({l, r});
	}

	sort(V.begin(), V.end());
	int p=0, q=0;
	vector<pii> VV;

	for(auto it : V)
	{
		if(q<it.first)
		{
			if(p) VV.push_back({p, q});
			p=it.first; q=it.second;
		}
		q=max(q, it.second);
	}
	if(p) VV.push_back({p, q});

	ans+=(VV.size()-1)*2;
	for(int i=0; i<VV.size() && VV[i].first==VV[i].second && VV[i].second<S; i++) ans-=2;
	for(int i=VV.size()-1; i>=0 && VV[i].first==VV[i].second && VV[i].first>S; i--) ans-=2;
	return ans;
}

Compilation message (stderr)

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:46:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=0; i<VV.size() && VV[i].first==VV[i].second && VV[i].second<S; i++) 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...