답안 #329187

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
329187 2020-11-19T16:13:59 Z arnold518 정렬하기 (IOI15_sorting) C++14
100 / 100
313 ms 28152 KB
#include "sorting.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 = 6e5;

int N, M, ans;
int S[MAXN+10], X[MAXN+10], Y[MAXN+10], *P, *Q;
int A[MAXN+10], B[MAXN+10], T[MAXN+10];
bool vis[MAXN+10];

bool solve(int R)
{	
	for(int i=1; i<=N; i++) A[i]=S[i];
	for(int i=1; i<=R; i++)
	{
		swap(A[X[i]], A[Y[i]]);
	}

	vector<pii> V;
	for(int i=1; i<=N; i++) vis[i]=false;

	//for(int i=1; i<=N; i++) printf("%d ", A[i]); printf("\n");
	for(int i=1; i<=N; i++)
	{
		if(vis[i]) continue;
		for(int now=i; ; now=A[now])
		{
			vis[now]=true;
			if(vis[A[now]]) break;
			V.push_back({A[now], A[A[now]]});
			//printf("!%d %d\n", A[now], A[A[now]]);
		}
	}
	if(V.size()>R) return false;

	for(int i=1; i<=N; i++) T[i]=i, A[i]=i;
	for(int i=1; i<=R; i++) P[i]=Q[i]=1;
	for(int i=R; i>=1; i--)
	{
		//for(int j=1; j<=N; j++) printf("%d ", A[j]); printf("\n");
		if(!V.empty())
		{
			pii t=V.back(); V.pop_back();
			pii a=pii(T[t.first], T[t.second]);
			P[i]=a.first; Q[i]=a.second;
			swap(A[a.first], A[a.second]);
			swap(T[t.first], T[t.second]);
		}

		//for(int j=1; j<=N; j++) printf("%d ", A[j]); printf("\n");

		pii a=pii(X[i], Y[i]);
		pii t=pii(A[a.first], A[a.second]);
		swap(A[a.first], A[a.second]);
		swap(T[t.first], T[t.second]);
	}
	//for(int j=1; j<=N; j++) printf("%d ", A[j]); printf("\n");

	for(int i=0; i<R; i++)
	{
		P[i]=P[i+1]-1;
		Q[i]=Q[i+1]-1;
	}
	return true;
}

int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[])
{
	N=_N; M=_M; P=_P; Q=_Q;
	for(int i=1; i<=N; i++) S[i]=_S[i-1]+1;
	for(int i=1; i<=M; i++) X[i]=_X[i-1]+1, Y[i]=_Y[i-1]+1;

	int lo=-1, hi=M;
	while(lo+1<hi)
	{
		int mid=lo+hi>>1;
		if(solve(mid)) hi=mid;
		else lo=mid;
	}
	for(int i=0; i<hi; i++)
	{
		swap(_S[_X[i]], _S[_Y[i]]);
		swap(_S[Q[i]], _S[P[i]]);
	}
	//for(int i=0; i<N; i++) printf("%d ", _S[i]);
	for(int i=0; i<N; i++) assert(i==_S[i]);
	return hi;
}

Compilation message

sorting.cpp: In function 'bool solve(int)':
sorting.cpp:39:13: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   39 |  if(V.size()>R) return false;
      |     ~~~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:81:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |   int mid=lo+hi>>1;
      |           ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 396 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 2 ms 748 KB Output is correct
24 Correct 2 ms 748 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 2 ms 748 KB Output is correct
27 Correct 2 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 2 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 2 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 234 ms 24908 KB Output is correct
16 Correct 294 ms 25440 KB Output is correct
17 Correct 283 ms 26664 KB Output is correct
18 Correct 98 ms 25356 KB Output is correct
19 Correct 196 ms 26928 KB Output is correct
20 Correct 213 ms 27416 KB Output is correct
21 Correct 192 ms 27480 KB Output is correct
22 Correct 236 ms 22044 KB Output is correct
23 Correct 260 ms 27876 KB Output is correct
24 Correct 304 ms 27344 KB Output is correct
25 Correct 313 ms 26904 KB Output is correct
26 Correct 213 ms 26136 KB Output is correct
27 Correct 190 ms 25552 KB Output is correct
28 Correct 274 ms 27220 KB Output is correct
29 Correct 285 ms 26832 KB Output is correct
30 Correct 146 ms 24936 KB Output is correct
31 Correct 293 ms 27480 KB Output is correct
32 Correct 261 ms 26708 KB Output is correct
33 Correct 297 ms 27364 KB Output is correct
34 Correct 267 ms 27248 KB Output is correct
35 Correct 197 ms 26480 KB Output is correct
36 Correct 67 ms 23916 KB Output is correct
37 Correct 301 ms 28152 KB Output is correct
38 Correct 278 ms 26892 KB Output is correct
39 Correct 290 ms 27068 KB Output is correct