답안 #803032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803032 2023-08-02T19:41:17 Z BT21tata 정렬하기 (IOI15_sorting) C++17
100 / 100
239 ms 22620 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;

bool used[200005];
int pos[200005], cur[200005];

bool check(int n, int S[], int m, int X[], int Y[], int P[], int Q[])
{
	for(int i=0; i<n; i++)
		cur[i]=S[i];
	for(int i=0; i<m; i++)
		swap(cur[X[i]], cur[Y[i]]);
	memset(used, 0, sizeof(used));
	vector<pair<int,int>>ans;
	//cout<<"check "<<m<<endl;
	for(int i=0; i<n; i++)
	{
		if(cur[i]!=i and !used[i])
		{
			vector<int>v; v.pb(i);
			int j=cur[i]; used[i]=1;
			while(j!=i)
			{
				v.pb(j);
				used[j]=1;
				j=cur[j];
			}
			//for(int u : v)cout<<u<<' ';cout<<endl;
			for(int j=0; j+1<v.size(); j++)
				ans.pb({v[j], v[j+1]});
		}
	}
	if(ans.size()>m) return 0;
	for(int i=0; i<n; i++)
		pos[S[i]]=i, cur[i]=S[i];
	for(int i=0; i<m; i++)
	{
		swap(cur[X[i]], cur[Y[i]]);
		swap(pos[cur[X[i]]], pos[cur[Y[i]]]);
		if(i<ans.size())
		{
			P[i]=pos[ans[i].F];
			Q[i]=pos[ans[i].S];
			swap(cur[P[i]], cur[Q[i]]);
			swap(pos[cur[P[i]]], pos[cur[Q[i]]]);
		}
		else P[i]=Q[i]=0;
	}
	return 1;
}

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	int l=0, r=n-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(n, S, mid, X, Y, P, Q))
			r=mid-1;
		else l=mid+1;
	}
	check(n, S, l, X, Y, P, Q);
	return l;
}
// 3 6 2 0 4 1 7 5 
// 1 6 7

Compilation message

sorting.cpp: In function 'bool check(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:33:12: warning: declaration of 'j' shadows a previous local [-Wshadow]
   33 |    for(int j=0; j+1<v.size(); j++)
      |            ^
sorting.cpp:25:8: note: shadowed declaration is here
   25 |    int j=cur[i]; used[i]=1;
      |        ^
sorting.cpp:33:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |    for(int j=0; j+1<v.size(); j++)
      |                 ~~~^~~~~~~~~
sorting.cpp:37:15: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |  if(ans.size()>m) return 0;
      |     ~~~~~~~~~~^~
sorting.cpp:44:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |   if(i<ans.size())
      |      ~^~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:56:39: warning: unused parameter 'M' [-Wunused-parameter]
   56 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                   ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 448 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 436 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 448 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 660 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 1 ms 576 KB Output is correct
5 Correct 1 ms 576 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 708 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 1 ms 576 KB Output is correct
5 Correct 1 ms 576 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 708 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 170 ms 19852 KB Output is correct
16 Correct 186 ms 20240 KB Output is correct
17 Correct 186 ms 21300 KB Output is correct
18 Correct 68 ms 16608 KB Output is correct
19 Correct 171 ms 18492 KB Output is correct
20 Correct 188 ms 21056 KB Output is correct
21 Correct 210 ms 20940 KB Output is correct
22 Correct 181 ms 16384 KB Output is correct
23 Correct 189 ms 22620 KB Output is correct
24 Correct 205 ms 21616 KB Output is correct
25 Correct 193 ms 21448 KB Output is correct
26 Correct 145 ms 20408 KB Output is correct
27 Correct 167 ms 18704 KB Output is correct
28 Correct 225 ms 21552 KB Output is correct
29 Correct 215 ms 20472 KB Output is correct
30 Correct 141 ms 17728 KB Output is correct
31 Correct 239 ms 20876 KB Output is correct
32 Correct 182 ms 21236 KB Output is correct
33 Correct 195 ms 21584 KB Output is correct
34 Correct 196 ms 20928 KB Output is correct
35 Correct 172 ms 18184 KB Output is correct
36 Correct 47 ms 16836 KB Output is correct
37 Correct 201 ms 22312 KB Output is correct
38 Correct 204 ms 21500 KB Output is correct
39 Correct 177 ms 21628 KB Output is correct