답안 #785230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
785230 2023-07-17T07:22:55 Z ymm 정렬하기 (IOI15_sorting) C++17
100 / 100
275 ms 22828 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef std::pair<int,int> pii;
typedef long long ll;
using namespace std;

const int maxn = 200010;
int pos[maxn];
int pospos[maxn];
int pos2[maxn];

void swap_by_idx(int a[], int p[], int i, int j)
{
	swap(p[a[i]], p[a[j]]);
	swap(a[i], a[j]);
}
void swap_by_val(int a[], int p[], int i, int j)
{
	swap_by_idx(a, p, p[i], p[j]);
}

bool can(int r, int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	Loop (i,0,N)
		pospos[i] = pos[i] = i;
	LoopR (i,0,r)
		swap_by_val(pos, pospos, X[i], Y[i]);
	Loop (i,0,N)
		pos2[S[i]] = i;
	vector<pii> vec;
	vector<int> where_should_go(N);
	Loop (i,0,N)
		where_should_go[pos[i]] = i;
	Loop (i,0,N) {
		while (where_should_go[pos2[i]] != i) {
			vec.push_back({pos2[i], pos2[where_should_go[pos2[i]]]});
			swap(pos2[i], pos2[where_should_go[pos2[i]]]);
		}
	}
	if (vec.size() > r)
		return 0;
	if (P && Q) {
		Loop (i,0,N)
			pos[i] = pospos[i] = i;
		Loop (i,0,vec.size()) {
			swap_by_val(pos, pospos, X[i], Y[i]);
			P[i] = pos[vec[i].first];
			Q[i] = pos[vec[i].second];
		}
		Loop (i,vec.size(),r)
			P[i] = Q[i] = 0;
	}
	return 1;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int l = 0, r = M;
	while (l < r) {
		int m = (l+r)/2;
		if (can(m, N, S, M, X, Y, 0, 0))
			r = m;
		else
			l = m+1;
	}
	can(l, N, S, M, X, Y, P, Q);
	return l;
}


Compilation message

sorting.cpp: In function 'bool can(int, int, int*, int, int*, int*, int*, int*)':
sorting.cpp:27:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   27 |   pospos[i] = pos[i] = i;
      |                        ^
sorting.cpp:31:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   31 |   pos2[S[i]] = i;
      |                ^
sorting.cpp:35:29: warning: conversion from 'll' {aka 'long long int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   35 |   where_should_go[pos[i]] = i;
      |                             ^
sorting.cpp:42:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |  if (vec.size() > r)
      |      ~~~~~~~~~~~^~~
sorting.cpp:46:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   46 |    pos[i] = pospos[i] = i;
      |                         ^
sorting.cpp:3:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    3 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
sorting.cpp:47:3: note: in expansion of macro 'Loop'
   47 |   Loop (i,0,vec.size()) {
      |   ^~~~
sorting.cpp:24:37: warning: unused parameter 'M' [-Wunused-parameter]
   24 | bool can(int r, int N, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                 ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 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 340 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 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 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 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 340 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 448 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 564 KB Output is correct
26 Correct 1 ms 448 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 448 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 416 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 448 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 416 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 205 ms 20400 KB Output is correct
16 Correct 228 ms 20752 KB Output is correct
17 Correct 275 ms 22064 KB Output is correct
18 Correct 70 ms 19356 KB Output is correct
19 Correct 146 ms 21064 KB Output is correct
20 Correct 159 ms 21656 KB Output is correct
21 Correct 155 ms 21736 KB Output is correct
22 Correct 213 ms 17172 KB Output is correct
23 Correct 250 ms 22564 KB Output is correct
24 Correct 256 ms 22340 KB Output is correct
25 Correct 252 ms 21996 KB Output is correct
26 Correct 161 ms 20348 KB Output is correct
27 Correct 145 ms 19700 KB Output is correct
28 Correct 260 ms 22408 KB Output is correct
29 Correct 254 ms 21684 KB Output is correct
30 Correct 99 ms 18756 KB Output is correct
31 Correct 234 ms 22184 KB Output is correct
32 Correct 225 ms 21828 KB Output is correct
33 Correct 274 ms 22276 KB Output is correct
34 Correct 272 ms 22256 KB Output is correct
35 Correct 156 ms 20812 KB Output is correct
36 Correct 54 ms 17388 KB Output is correct
37 Correct 257 ms 22828 KB Output is correct
38 Correct 246 ms 22004 KB Output is correct
39 Correct 256 ms 22124 KB Output is correct