답안 #485848

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485848 2021-11-09T14:21:18 Z MilosMilutinovic 정렬하기 (IOI15_sorting) C++14
74 / 100
1000 ms 17220 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;
int n,a[N],s[N],id[N],pos[N],x[N],y[N];

bool check(int r) {
	rep(i,0,n) a[i]=s[i],id[i]=i;
	rep(i,0,r) {
		swap(a[x[i]],a[y[i]]);
	}
	rep(i,0,n) pos[a[i]]=i;
	int cnt=0;
	rep(i,0,n) {
		if (pos[i]==i) continue;
		int w=a[i];
		swap(a[pos[i]],a[i]);
		swap(pos[i],pos[w]);
		cnt++;
	}
	return cnt<=r;
}

void gao(int r,int* p,int* q) {
	rep(i,0,n) a[i]=s[i],id[i]=i;
	rep(i,0,r) {
		swap(a[x[i]],a[y[i]]);
	}
	rep(i,0,n) pos[a[i]]=i;
	int cnt=0;
	vector<PII> todo;
	rep(i,0,n) {
		if (pos[i]==i) continue;
		todo.pb(mp(i,a[i]));
		int w=a[i];
		swap(a[pos[i]],a[i]);
		swap(pos[i],pos[w]);
	}
	assert(is_sorted(a,a+n));
	rep(i,0,n) a[i]=s[i];
	rep(i,0,r) {
		swap(a[x[i]],a[y[i]]);
		if (i<SZ(todo)) {
			rep(j,0,n) if (a[j]==todo[i].fi) p[i]=j;
			rep(j,0,n) if (a[j]==todo[i].se) q[i]=j;
			swap(a[p[i]],a[q[i]]);	
		} else p[i]=q[i]=0;
	}
	//rep(i,0,n) printf("%d ",a[i]); puts("");
	//assert(is_sorted(a,a+n));
	rep(i,SZ(todo),r) p[i]=q[i]=0;
}

int findSwapPairs(int n,int* s,int m,int* x,int* y,int* p,int* q) {
	if (is_sorted(s,s+n)) return 0;
	::n=n;
	rep(i,0,n) ::s[i]=s[i];
	rep(i,0,m) ::x[i]=x[i],::y[i]=y[i];
	int ans=1;
	per(i,1,n+1) if (check(i)) ans=i;
	//printf("%d ",ans);
	gao(ans,p,q);
	return ans;
}

Compilation message

sorting.cpp: In function 'int rnd(int)':
sorting.cpp:18:33: warning: conversion from 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   18 | int rnd(int x) { return mrand() % x;}
      |                         ~~~~~~~~^~~
sorting.cpp: In function 'void gao(int, int*, int*)':
sorting.cpp:49:6: warning: unused variable 'cnt' [-Wunused-variable]
   49 |  int cnt=0;
      |      ^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:73:50: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int n,int* s,int m,int* x,int* y,int* p,int* q) {
      |                                             ~~~~~^
sorting.cpp:24:35: note: shadowed declaration is here
   24 | int n,a[N],s[N],id[N],pos[N],x[N],y[N];
      |                                   ^
sorting.cpp:73:43: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int n,int* s,int m,int* x,int* y,int* p,int* q) {
      |                                      ~~~~~^
sorting.cpp:24:30: note: shadowed declaration is here
   24 | int n,a[N],s[N],id[N],pos[N],x[N],y[N];
      |                              ^
sorting.cpp:73:30: warning: declaration of 's' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int n,int* s,int m,int* x,int* y,int* p,int* q) {
      |                         ~~~~~^
sorting.cpp:24:12: note: shadowed declaration is here
   24 | int n,a[N],s[N],id[N],pos[N],x[N],y[N];
      |            ^
sorting.cpp:73:23: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int n,int* s,int m,int* x,int* y,int* p,int* q) {
      |                   ~~~~^
sorting.cpp:24:5: note: shadowed declaration is here
   24 | int n,a[N],s[N],id[N],pos[N],x[N],y[N];
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 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 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 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 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 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 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 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 3 ms 588 KB Output is correct
22 Correct 3 ms 588 KB Output is correct
23 Correct 3 ms 588 KB Output is correct
24 Correct 3 ms 588 KB Output is correct
25 Correct 3 ms 700 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 3 ms 572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 480 KB Output is correct
2 Correct 35 ms 532 KB Output is correct
3 Correct 33 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 29 ms 532 KB Output is correct
6 Correct 28 ms 520 KB Output is correct
7 Correct 42 ms 460 KB Output is correct
8 Correct 34 ms 460 KB Output is correct
9 Correct 35 ms 508 KB Output is correct
10 Correct 34 ms 520 KB Output is correct
11 Correct 38 ms 528 KB Output is correct
12 Correct 31 ms 528 KB Output is correct
13 Correct 34 ms 508 KB Output is correct
14 Correct 20 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 480 KB Output is correct
2 Correct 35 ms 532 KB Output is correct
3 Correct 33 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 29 ms 532 KB Output is correct
6 Correct 28 ms 520 KB Output is correct
7 Correct 42 ms 460 KB Output is correct
8 Correct 34 ms 460 KB Output is correct
9 Correct 35 ms 508 KB Output is correct
10 Correct 34 ms 520 KB Output is correct
11 Correct 38 ms 528 KB Output is correct
12 Correct 31 ms 528 KB Output is correct
13 Correct 34 ms 508 KB Output is correct
14 Correct 20 ms 460 KB Output is correct
15 Execution timed out 1095 ms 17220 KB Time limit exceeded
16 Halted 0 ms 0 KB -