답안 #723039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
723039 2023-04-13T07:50:16 Z sunwukong123 정렬하기 (IOI15_sorting) C++14
0 / 100
2 ms 468 KB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach 
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first 
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 200005;
int A[MAXN];
int n,m;
int X[MAXN], Y[MAXN];
int idx[MAXN];
struct ufds_{
	int p[MAXN],sz[MAXN];
	ufds_() {
		iota(p,p+MAXN,0);
		fill(sz,sz+MAXN,1);
	}
	int find(int x) {
		if(x==p[x])return x;
		else return p[x]=find(p[x]);
	}
	void merge(int x, int y) {
		x=find(x);y=find(y);
		if(x==y)return;
		if(sz[x]>=sz[y]){
			sz[x]+=sz[y];
			p[y]=x;
		} else {
			sz[y]+=sz[x];
			p[x]=y;
		}
	}
};
bool check(int ti) {
	//you can use ti operations to do everything
	
	FOR(i,0,ti-1) {
		swap(A[X[i]],A[Y[i]]);
	}
	FOR(i,0,n-1){
		idx[A[i]]=i;
	}
	int res = 0;
	dba(A,0,n-1);
	FOR(i,0,n-1) {
		int y=idx[i];
		if(i==y)continue;
		else {
			// swap index i to index y.
			db2(i,y);
			++res;
			swap(A[i],A[y]);
			swap(idx[A[i]],idx[A[y]]);
		}
	}
	return res<=ti;

}
int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	//FOR(i,0,n-1)A[i]=S[i];
	::n=n;
	::m=M;
	FOR(i,0,m-1)::X[i]=X[i];
	FOR(i,0,m-1)::Y[i]=Y[i];
	int lo=0, hi=m+1;
	while(lo<hi-1) {
		FOR(i,0,n-1)A[i]=S[i];
		int mid=(lo+hi)/2;
		if(check(mid))hi=mid;
		else lo=mid;

	}
	int R=hi;
	db(R);
	// i am doing R number of operations.
	FOR(i,0,n-1)A[i]=S[i];
	FOR(i,0,R-1) {
		swap(A[X[i]],A[Y[i]]);
	}
	vector<pi> ops;
	FOR(i,0,n-1){
		idx[A[i]]=i;
	}
	FOR(i,0,n-1) {
		int y=idx[i];
		if(i==y)continue;
		else {
			// swap index i to index y.
			db2(A[i],A[y]);
			ops.pb({A[i],A[y]});
			swap(A[i],A[y]);
			swap(idx[A[i]],idx[A[y]]);
		}
	}

	reverse(all(ops));
	FOR(i,0,n-1)A[i]=S[i];
	dba(A,0,n-1);
	FOR(i,0,n-1)idx[A[i]]=i;

	FOR(i,0,R-1) {
		pi cur=ops.back(); ops.pop_back();
		int val1=cur.f, val2=cur.s;
		int Aidx=idx[val1], Bidx=idx[val2];
		
		db2(Aidx,Bidx);

		P[i]=Aidx; Q[i]=Bidx;

		swap(A[Aidx],A[Bidx]);
		swap(idx[val1],idx[val2]);

		dba(A,0,n-1);
		db2(X[i],Y[i]);
		swap(A[X[i]],A[Y[i]]);
		swap(idx[A[X[i]]], idx[A[Y[i]]]);

		dba(A,0,n-1);
	}
	dba(P,0,R-1);
	dba(Q,0,R-1);
	return R;


}







Compilation message

sorting.cpp: In function 'int rand(int, int)':
sorting.cpp:36:35: 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]
   36 | int rand(int a, int b) { return a + rng() % (b-a+1); }
      |                                 ~~^~~~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:94:55: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
   94 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                   ~~~~^~~
sorting.cpp:45:14: note: shadowed declaration is here
   45 | int X[MAXN], Y[MAXN];
      |              ^
sorting.cpp:94:46: warning: declaration of 'X' shadows a global declaration [-Wshadow]
   94 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                          ~~~~^~~
sorting.cpp:45:5: note: shadowed declaration is here
   45 | int X[MAXN], Y[MAXN];
      |     ^
sorting.cpp:94:23: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   94 | int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:44:5: note: shadowed declaration is here
   44 | int n,m;
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -