답안 #832486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832486 2023-08-21T10:51:11 Z Rafi22 고대 책들 (IOI17_books) C++14
0 / 100
1 ms 340 KB
#include <bits/stdc++.h>
     
using namespace std;
     
#define ll long long 
#define ld long double
#define pb push_back
#define st first
#define nd second
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
int inf=1000000007;
ll infl=1000000000000000007;
     
const int N=1000007;
     
bool odw[N];
int id[N];
vector<pair<int,int>>V;
    
    
ll minimum_walk(vector<int>p,int s)
{
	int n=sz(p);
	ll ans=0;
	for(int i=0;i<n;i++) ans+=abs(i-p[i]);
	int c=0;
	int A=0,B=n-1;
	while(A<s&&p[A]==A) A++;
	while(s<B&&p[B]==B) B--;
	for(int i=0;i<n;i++)
	{
		if(odw[i]) continue;
		int l=inf,r=-inf,x=i;
		vector<int>X;
		while(!odw[x])
		{
			odw[x]=1;
			X.pb(x);
			l=min(l,x);
			r=max(r,x);
			x=p[x];
		}
		if(l==r&&(l<A||l>B)) continue;   		
		V.pb({l,r});
		for(auto x:X) id[x]=c;
		c++;
	}
	int i=s,j=s;
	int l=V[id[s]].st,r=V[id[s]].nd;
	while(l>A||r<B)
	{
		while(true)
		{
			if(i>l) 
			{
				i--;
				l=min(l,V[id[i]].st);
				r=max(r,V[id[i]].nd);
				
			}
			else if(j<r)
			{
				j++;
				l=min(l,V[id[j]].st);
				r=max(r,V[id[j]].nd);
			}
			else break;
		}
		if(l==A&&r==B) break;
		int cL=1,L=-1,p=inf;
		for(int k=l-1;k>=A;k--)
		{
			p=min(p,V[id[k]].st);
			if(V[id[k]].nd>r)
			{
				 L=k;
				 break;
			}
			if(p==k) cL++;
		}
		p=-inf;
		int cR=1,R=-1;
		for(int k=r+1;k<=B;k++)
		{
			p=max(p,V[id[k]].nd);
			if(V[id[k]].st<l)
			{
				 R=k;
				 break;
			}
			if(p==k) cR++;
		}
		if(L==-1)
		{
			ans+=cL+cR;
			break;
		}
		l=L;
		r=R;
		ans+=min(cL,cR);
	}
    	
	return ans;
}
/*
int main()
{
	int nn,s;
	cin>>nn>>s;
	vector<int>V(nn);
	for(int i=0;i<nn;i++) cin>>V[i];
	//random_shuffle(all(V));
	//for(int i=0;i<nn;i++) cout<<V[i]<<" ";
	//cout<<endl;
	cout<<minimum_walk(V,s);
	return 0;
}*/
    
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 308 KB 3rd lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 308 KB 3rd lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 308 KB 3rd lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3024'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 308 KB 3rd lines differ - on the 1st token, expected: '6', found: '7'
2 Halted 0 ms 0 KB -