답안 #676035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676035 2022-12-29T01:28:42 Z atcgu 정렬하기 (IOI15_sorting) C++14
100 / 100
276 ms 33840 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000010;

typedef pair<int, int> ii;
typedef long long ll;

int n, m;
vector<int> ar;

int ar1[MAXN], ar2[MAXN];

vector<ii> ans;

bool check(int t) {
	vector<int> cur = ar;
	for (int i = 0; i < t; i++) {
		swap(cur[ar1[i]], cur[ar2[i]]);
	}
	vector<ii> swaps;
	for (int i = 0; i < n; i++) {
		while (cur[i] != i) {
			swaps.emplace_back(cur[i], cur[cur[i]]);
			swap(cur[i], cur[cur[i]]);
		}
	}
	while (swaps.size() < t) swaps.emplace_back(0, 0);
	if (swaps.size() > t) return false;
	cur = ar;
	vector<int> ind(n);
	for (int i = 0; i < n; i++) {
		ind[cur[i]] = i;
	}
	vector<ii> ret;
	for (int i = 0; i < t; i++) {
		int x = ar1[i];
		int y = ar2[i];
		swap(ind[cur[x]], ind[cur[y]]);
		swap(cur[x], cur[y]);

		x = swaps[i].first;
		y = swaps[i].second;
		ret.emplace_back(ind[x], ind[y]);
		swap(cur[ind[x]], cur[ind[y]]);
		swap(ind[x], ind[y]);
	}
	ans = ret;
	return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N;
	ar = vector<int>(n);
  	for (int i = 0; i < n; i++) ar[i] = S[i];
  	m = M;
  	for (int i = 0; i < m; i++) {
      ar1[i] = X[i];
      ar2[i] = Y[i];
    }
	int lo = 0, hi = m;
	while (lo < hi) {
		int mid = lo + (hi - lo) / 2;
		if (check(mid)) {
			hi = mid;
		}
		else lo = mid + 1;
	}
	check(lo);
	for (int i = 0; i < ans.size(); i++) {
      P[i] = ans[i].first;
      Q[i] = ans[i].second;
	}
  	return ans.size();
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:29:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |  while (swaps.size() < t) swaps.emplace_back(0, 0);
      |         ~~~~~~~~~~~~~^~~
sorting.cpp:30: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]
   30 |  if (swaps.size() > t) return false;
      |      ~~~~~~~~~~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < ans.size(); i++) {
      |                  ~~^~~~~~~~~~~~
sorting.cpp:75:19: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   75 |    return ans.size();
      |           ~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 312 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Correct 0 ms 308 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 312 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Correct 0 ms 308 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 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 320 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 312 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Correct 0 ms 308 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 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 320 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 852 KB Output is correct
22 Correct 2 ms 832 KB Output is correct
23 Correct 2 ms 836 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 840 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 1 ms 584 KB Output is correct
5 Correct 1 ms 580 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 3 ms 644 KB Output is correct
10 Correct 2 ms 580 KB Output is correct
11 Correct 2 ms 580 KB Output is correct
12 Correct 2 ms 580 KB Output is correct
13 Correct 2 ms 576 KB Output is correct
14 Correct 1 ms 580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 1 ms 584 KB Output is correct
5 Correct 1 ms 580 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 3 ms 644 KB Output is correct
10 Correct 2 ms 580 KB Output is correct
11 Correct 2 ms 580 KB Output is correct
12 Correct 2 ms 580 KB Output is correct
13 Correct 2 ms 576 KB Output is correct
14 Correct 1 ms 580 KB Output is correct
15 Correct 209 ms 29468 KB Output is correct
16 Correct 220 ms 30152 KB Output is correct
17 Correct 264 ms 32012 KB Output is correct
18 Correct 97 ms 28184 KB Output is correct
19 Correct 178 ms 30336 KB Output is correct
20 Correct 184 ms 31360 KB Output is correct
21 Correct 166 ms 31388 KB Output is correct
22 Correct 194 ms 26996 KB Output is correct
23 Correct 254 ms 32644 KB Output is correct
24 Correct 264 ms 32384 KB Output is correct
25 Correct 249 ms 32480 KB Output is correct
26 Correct 180 ms 31196 KB Output is correct
27 Correct 166 ms 30308 KB Output is correct
28 Correct 227 ms 32176 KB Output is correct
29 Correct 242 ms 32164 KB Output is correct
30 Correct 128 ms 29932 KB Output is correct
31 Correct 276 ms 32784 KB Output is correct
32 Correct 210 ms 31688 KB Output is correct
33 Correct 258 ms 32708 KB Output is correct
34 Correct 258 ms 32352 KB Output is correct
35 Correct 197 ms 29932 KB Output is correct
36 Correct 77 ms 29772 KB Output is correct
37 Correct 261 ms 33840 KB Output is correct
38 Correct 246 ms 32364 KB Output is correct
39 Correct 255 ms 32876 KB Output is correct