답안 #587520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587520 2022-07-02T04:23:49 Z benson1029 정렬하기 (IOI15_sorting) C++14
0 / 100
3 ms 468 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;
			}
			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:76:1: warning: control reaches end of non-void function [-Wreturn-type]
   76 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -