답안 #99532

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
99532 2019-03-04T17:27:47 Z MvC 고대 책들 (IOI17_books) C++11
0 / 100
10 ms 384 KB
#pragma GCC optimize("O3")
#include "books.h"
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int n,s,p[nmax],mn,mx,t,i,st[nmax],v[nmax],j;
ll ans,nr;
ll minimum_walk(vector<int>x,int s)
{
	n=(int)x.size();
	for(i=1;i<=n;i++)p[i]=x[i-1]+1;
	for(i=1;i<=n;i++)ans+=1LL*abs(i-p[i]);
	for(i=1;i<=n;i++)
	{
		if(v[p[i]])continue;
		j=p[i];
		mn=1e9,mx=-1;
		while(!v[j])
		{
			v[j]=1;
			mn=min(mn,j);
			mx=max(mx,j);
			j=p[j];
		}
		st[mn]=mx;
		if(mn!=mx)t=max(t,mx);
	}
	for(i=1;i<=t;i++)
	{
		mx=max(mx,st[i]);
		if(i==mx)ans+=2LL;
	}
	if(!t)return 0LL;
	return ans-2LL;
}
/*int main()
{
	//freopen("sol.in","r",stdin);
	//freopen("sol.out","w",stdout);
	ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
	cin>>n>>s;
	for(i=1;i<=n;i++)cin>>p[i];
	for(i=1;i<=n;i++)ans+=abs(i-p[i]);
	for(i=1;i<=n;i++)
	{
		if(v[p[i]])continue;
		j=p[i];
		mn=1e9,mx=-1;
		while(!v[j])
		{
			v[j]=1;
			mn=min(mn,j);
			mx=max(mx,j);
			j=p[j];
		}
		st[mn]=mx;
		t=max(t,mx);
	}
	mx=st[1];
	for(i=1;i<=t;i++)
	{
		if(mx<st[i])nr++;
		mx=max(mx,st[i]);
	}
	cout<<ans+2*nr<<endl;
    return 0;
}
/*
4 1
1 3 4 2
*/

Compilation message

books.cpp:81:1: warning: "/*" within comment [-Wcomment]
 /*
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 356 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -