답안 #785461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
785461 2023-07-17T09:20:43 Z fatemetmhr 정렬하기 (IOI15_sorting) C++17
100 / 100
146 ms 19944 KB
//  ~ Be Name Khoda ~  //

#include "sorting.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  6e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 7;
const ll  inf   =  1e18;


int n, a[maxn5], b[maxn5], x[maxn5], y[maxn5];
int mask[maxn5], rev[maxn5];
bool mark[maxn5], add = false;
vector <pair<int, int>> op;


int rt;

void dfs(int v){
	mark[v] = true;
	if(!mark[b[v]]){
		//cout << "its " << v << ' ' << b[v] << endl;
		if(add)
			op.pb({rt, b[v]});
		dfs(b[v]);
	}
}

bool check(int m){
	//cout << "hereeeeee " << m << endl;
	memset(mark, false, sizeof mark);
	for(int i = 0; i < n; i++){
		b[i] = a[i];
	}
	for(int i = 0; i < m; i++)
		swap(b[x[i]], b[y[i]]);
	int ans = n;
	for(int i = 0; i < n; i++) if(!mark[i]){
		rt = i;
		dfs(i);
		ans--;
	}
	//cout << "in " << m << ' ' << pt << endl;
	return ans <= m;
}



int findSwapPairs(int N, int A[], int m, int X[], int Y[], int p[], int q[]) {
	n = N;
	for(int i = 0; i < m; i++){
		x[i] = X[i];
		y[i] = Y[i];
		if(x[i] > y[i])
			swap(x[i], y[i]);
	}
	for(int i = 0; i < n; i++)
		a[i] = A[i];
	bool re = true;
	for(int i = 1; i < n; i++)
		re &= (a[i] > a[i - 1]);
	if(re)
		return 0;
    int lo = -1, hi = m;
    while(hi - lo > 1){
    	int mid = (hi + lo) >> 1;
    	if(check(mid))
    		hi = mid;
    	else
    		lo = mid;
    }
    add = true;
    check(hi);
    for(int i = 0; i < n; i++)
    	mask[i] = rev[i] = i;
    for(int i = 0; i < hi; i++)
    	q[i] = p[i] = 0;
    for(int i = hi - 1; i >= 0; i--){
    	if(op.size() > i){
    		p[i] = mask[op.back().fi];
    		q[i] = mask[op.back().se];
    		op.pop_back();
    	}
    	int a = rev[x[i]], b = rev[y[i]];
    	mask[a] = y[i];
    	mask[b] = x[i];
    	rev[x[i]] = b;
    	rev[y[i]] = a;
    }
    return hi;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:95:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   95 |      if(op.size() > i){
      |         ~~~~~~~~~~^~~
sorting.cpp:100:10: warning: declaration of 'a' shadows a global declaration [-Wshadow]
  100 |      int a = rev[x[i]], b = rev[y[i]];
      |          ^
sorting.cpp:27:8: note: shadowed declaration is here
   27 | int n, a[maxn5], b[maxn5], x[maxn5], y[maxn5];
      |        ^
sorting.cpp:100:25: warning: declaration of 'b' shadows a global declaration [-Wshadow]
  100 |      int a = rev[x[i]], b = rev[y[i]];
      |                         ^
sorting.cpp:27:18: note: shadowed declaration is here
   27 | int n, a[maxn5], b[maxn5], x[maxn5], y[maxn5];
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 980 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 1 ms 980 KB Output is correct
17 Correct 1 ms 980 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 244 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 2 ms 1108 KB Output is correct
23 Correct 2 ms 1128 KB Output is correct
24 Correct 2 ms 1108 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 2 ms 1108 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 2 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 2 ms 1108 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 2 ms 1108 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 2 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 2 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 2 ms 1108 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 104 ms 17064 KB Output is correct
16 Correct 112 ms 17412 KB Output is correct
17 Correct 135 ms 18724 KB Output is correct
18 Correct 46 ms 10316 KB Output is correct
19 Correct 114 ms 16624 KB Output is correct
20 Correct 124 ms 17216 KB Output is correct
21 Correct 120 ms 17256 KB Output is correct
22 Correct 99 ms 18492 KB Output is correct
23 Correct 132 ms 18892 KB Output is correct
24 Correct 144 ms 18852 KB Output is correct
25 Correct 131 ms 19080 KB Output is correct
26 Correct 133 ms 17080 KB Output is correct
27 Correct 116 ms 16324 KB Output is correct
28 Correct 128 ms 18492 KB Output is correct
29 Correct 139 ms 18476 KB Output is correct
30 Correct 93 ms 15520 KB Output is correct
31 Correct 137 ms 18900 KB Output is correct
32 Correct 129 ms 18268 KB Output is correct
33 Correct 146 ms 19084 KB Output is correct
34 Correct 142 ms 18800 KB Output is correct
35 Correct 124 ms 16468 KB Output is correct
36 Correct 55 ms 14020 KB Output is correct
37 Correct 143 ms 19944 KB Output is correct
38 Correct 128 ms 18888 KB Output is correct
39 Correct 126 ms 19380 KB Output is correct