답안 #519036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519036 2022-01-25T12:02:48 Z ymm 정렬하기 (IOI15_sorting) C++17
100 / 100
215 ms 20256 KB
///
///   Oh? You're approaching me?
///

#include <bits/stdc++.h>
#define Loop(x,l,r) for(ll x = ll(l); x < ll(r); ++x)
#define LoopR(x,l,r) for(ll x = ll(r)-1; x >= ll(l); --x)
#define Kill(x) exit((cout << (x) << '\n', 0))
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
using namespace std;

#include "sorting.h"

const int N = 2e5+10;
int a[N], b[N];
int *S, *X, *Y, *P, *Q;
int n, m;

void init(int* s){
	Loop(i,0,n) a[i] = s[i], b[s[i]] = i;
}

void swp(int i, int j){
	swap(b[a[i]], b[a[j]]);
	swap(a[i], a[j]);
}

bool can(int k){
	init(S);
	Loop(i,0,k)
		swp(X[i], Y[i]);
	int cnt=0;
	static bitset<N> vis;
	vis.reset();
	Loop(i,0,n){
		if(vis[i]) continue;
		++cnt;
		int j=i;
		while(!vis[j])
			vis[j]=1, j=a[j];
	}
	return n-cnt<=k;
}

void solve(int k){
	init(S);
	Loop(i,0,k)
		swp(X[i], Y[i]);
	for(int i=0,j=0; i<n; ++i){
		if(b[i] != i){
			P[j] = a[i];
			Q[j] = a[b[i]];
			swp(i, b[i]);
			++j;
		}
	}
	init(S);
	Loop(i,0,k){
		swp(X[i], Y[i]);
		Q[i] = b[Q[i]];
		P[i] = b[P[i]];
		swp(P[i], Q[i]);
	}
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n=N; m=M; ::S=S; ::X=X; ::Y=Y; ::P=P; ::Q=Q;
	int l=0, r=m;
	while(l<r){
		int m=(l+r)/2;
		if(can(m)) r=m;
		else       l=m+1;
	}
	solve(l);
	return l;
}

Compilation message

sorting.cpp: In function 'void init(int*)':
sorting.cpp:22:37: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   22 |  Loop(i,0,n) a[i] = s[i], b[s[i]] = i;
      |                                     ^
sorting.cpp: In function 'bool can(int)':
sorting.cpp:40:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   40 |   int j=i;
      |         ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:68:73: warning: declaration of 'Q' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                                     ~~~~^~~
sorting.cpp:18:22: note: shadowed declaration is here
   18 | int *S, *X, *Y, *P, *Q;
      |                      ^
sorting.cpp:68:64: warning: declaration of 'P' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                            ~~~~^~~
sorting.cpp:18:18: note: shadowed declaration is here
   18 | int *S, *X, *Y, *P, *Q;
      |                  ^
sorting.cpp:68:55: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                   ~~~~^~~
sorting.cpp:18:14: note: shadowed declaration is here
   18 | int *S, *X, *Y, *P, *Q;
      |              ^
sorting.cpp:68:46: warning: declaration of 'X' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                          ~~~~^~~
sorting.cpp:18:10: note: shadowed declaration is here
   18 | int *S, *X, *Y, *P, *Q;
      |          ^
sorting.cpp:68:30: warning: declaration of 'S' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                          ~~~~^~~
sorting.cpp:18:6: note: shadowed declaration is here
   18 | int *S, *X, *Y, *P, *Q;
      |      ^
sorting.cpp:68:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   68 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:16:11: note: shadowed declaration is here
   16 | const int N = 2e5+10;
      |           ^
sorting.cpp:72:7: warning: declaration of 'm' shadows a global declaration [-Wshadow]
   72 |   int m=(l+r)/2;
      |       ^
sorting.cpp:19:8: note: shadowed declaration is here
   19 | int n, m;
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 440 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 2 ms 568 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 1 ms 556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 440 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 496 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 440 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 496 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 134 ms 17928 KB Output is correct
16 Correct 164 ms 18272 KB Output is correct
17 Correct 212 ms 19396 KB Output is correct
18 Correct 80 ms 14772 KB Output is correct
19 Correct 163 ms 17620 KB Output is correct
20 Correct 170 ms 18312 KB Output is correct
21 Correct 169 ms 18308 KB Output is correct
22 Correct 127 ms 14660 KB Output is correct
23 Correct 159 ms 20052 KB Output is correct
24 Correct 210 ms 19816 KB Output is correct
25 Correct 209 ms 19508 KB Output is correct
26 Correct 174 ms 18372 KB Output is correct
27 Correct 146 ms 17540 KB Output is correct
28 Correct 208 ms 19700 KB Output is correct
29 Correct 192 ms 19084 KB Output is correct
30 Correct 124 ms 16980 KB Output is correct
31 Correct 207 ms 19440 KB Output is correct
32 Correct 166 ms 19340 KB Output is correct
33 Correct 214 ms 19712 KB Output is correct
34 Correct 192 ms 19664 KB Output is correct
35 Correct 157 ms 17468 KB Output is correct
36 Correct 69 ms 15936 KB Output is correct
37 Correct 215 ms 20256 KB Output is correct
38 Correct 207 ms 19476 KB Output is correct
39 Correct 208 ms 19604 KB Output is correct