답안 #519033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519033 2022-01-25T12:00:42 Z ymm 정렬하기 (IOI15_sorting) C++17
0 / 100
1 ms 332 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]);
	int j=0;
	for(int i=0; i<n; ++i){
		if(b[i] != i){
			P[j] = a[i];
			Q[j] = a[b[i]];
			swp(i, b[i]);
			++j;
		}
	}
	assert(j==k);
	init(S);
	Loop(i,0,k){
		swp(X[i], Y[i]);
		P[i] = b[Q[i]];
		Q[i] = b[P[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:69:73: warning: declaration of 'Q' shadows a global declaration [-Wshadow]
   69 | 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:69:64: warning: declaration of 'P' shadows a global declaration [-Wshadow]
   69 | 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:69:55: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
   69 | 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:69:46: warning: declaration of 'X' shadows a global declaration [-Wshadow]
   69 | 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:69:30: warning: declaration of 'S' shadows a global declaration [-Wshadow]
   69 | 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:69:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   69 | 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:73:7: warning: declaration of 'm' shadows a global declaration [-Wshadow]
   73 |   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 332 KB Output is correct
5 Incorrect 0 ms 204 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 332 KB Output is correct
5 Incorrect 0 ms 204 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Incorrect 0 ms 332 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 332 KB Output is correct
5 Incorrect 0 ms 204 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -