답안 #587524

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587524 2022-07-02T04:37:15 Z benson1029 정렬하기 (IOI15_sorting) C++14
100 / 100
376 ms 23732 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int a[200005], b[200005], s[200005];
bool stexists[200005];

vector< pair<int,int> > v;
queue<int> st;

void swapa(int x, int y) {
	b[a[x]] = y;
	b[a[y]] = x;
	if(!stexists[x] && stexists[y]) {
		st.push(x);
	}
	else if(stexists[x] && !stexists[y]) {
		st.push(y);
	}
	swap(stexists[x], stexists[y]);
	swap(a[x], a[y]);
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int l = 0, r = N, mid;

	while(l <= r) {
		mid = (l+r)/2;
		
		for(int i=0; i<N; i++) {
			a[i] = i;
			s[i] = S[i];
		}
		for(int i=mid-1; i>=0; i--) {
			swap(a[X[i]], a[Y[i]]);
		}
		while(!st.empty()) st.pop();
		for(int i=0; i<N; i++) {
			stexists[i] = (a[i] != s[i]);
			if(a[i] != s[i]) st.push(i);
			b[a[i]] = i;
		}
		v.clear();

		for(int i=0; i<mid; i++) {
			swapa(X[i], Y[i]);
			swap(s[X[i]], s[Y[i]]);
			int j = 200001;
			while(!st.empty() && !stexists[j]) {
				j = st.front(); st.pop();
			}
			if(!stexists[j]) {
				v.push_back({0, 0});
				continue;
			}
			int k = b[s[j]];
			swap(s[j], s[k]);
			v.push_back({j, k});
			if(s[j] == a[j]) {
				stexists[j] = false;
			} else st.push(j);
			if(s[k] == a[k]) {
				stexists[k] = false;
			}
		}

		if(l==r) {
			for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
			return v.size();
		} else {
			while(!st.empty() && !stexists[st.front()]) {
				st.pop();
			}
			if(st.empty()) r = mid;
			else l = mid+1;
		}
	}
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |    for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
      |                 ~^~~~~~~~~
sorting.cpp:69:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   69 |    return v.size();
      |           ~~~~~~^~
sorting.cpp:24:39: warning: unused parameter 'M' [-Wunused-parameter]
   24 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:78:1: warning: control reaches end of non-void function [-Wreturn-type]
   78 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 0 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 340 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 1 ms 308 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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 0 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 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 308 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 212 KB Output is correct
21 Correct 1 ms 572 KB Output is correct
22 Correct 2 ms 444 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 552 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 448 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 2 ms 452 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 3 ms 448 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 2 ms 452 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 269 ms 13576 KB Output is correct
16 Correct 312 ms 21412 KB Output is correct
17 Correct 376 ms 22560 KB Output is correct
18 Correct 86 ms 17384 KB Output is correct
19 Correct 212 ms 20008 KB Output is correct
20 Correct 274 ms 21000 KB Output is correct
21 Correct 274 ms 21052 KB Output is correct
22 Correct 271 ms 17944 KB Output is correct
23 Correct 276 ms 23576 KB Output is correct
24 Correct 374 ms 23112 KB Output is correct
25 Correct 361 ms 22848 KB Output is correct
26 Correct 227 ms 20716 KB Output is correct
27 Correct 198 ms 19792 KB Output is correct
28 Correct 346 ms 22820 KB Output is correct
29 Correct 323 ms 22136 KB Output is correct
30 Correct 153 ms 19264 KB Output is correct
31 Correct 339 ms 22652 KB Output is correct
32 Correct 299 ms 22572 KB Output is correct
33 Correct 357 ms 22988 KB Output is correct
34 Correct 318 ms 22960 KB Output is correct
35 Correct 250 ms 19816 KB Output is correct
36 Correct 68 ms 17968 KB Output is correct
37 Correct 367 ms 23732 KB Output is correct
38 Correct 338 ms 22792 KB Output is correct
39 Correct 355 ms 22832 KB Output is correct