제출 #1030340

#제출 시각아이디문제언어결과실행 시간메모리
1030340pcc정렬하기 (IOI15_sorting)C++17
20 / 100
4 ms604 KiB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define fs first
#define sc second

const int mxn = 2e5+10;
pii arr[mxn];
int perm[mxn],pos[mxn];
int N,M;
int done = 0;

void del(int k){
	done -= (perm[k] == k);
	return;
}
void add(int k){
	done += (perm[k] == k);
}
bool check(){
	for(int i = 0;i<N;i++){
		if(perm[i] != i)return false;
	}
	return true;
}

void act(int a,int b){
	if(a == b)return;
	del(a);del(b);
	swap(perm[a],perm[b]);
	pos[perm[a]] = a;
	pos[perm[b]] = b;
	add(a);add(b);
	return;
}

void OP(int id){
	auto [a,b] = arr[id];
	act(a,b);
	return;
}

int findSwapPairs(int NN, int S[], int MM, int X[], int Y[], int P[], int Q[]) {
	N = NN;M = MM;
	for(int i = 0;i<N;i++){
		perm[i] = S[i];
		pos[perm[i]] = i;
		add(i);
	}
	for(int i = 0;i<M;i++)arr[i] = pii(X[i],Y[i]);
	if(check())return 0;

	for(int i = 0;i+1<N;i++){
		P[i] = Q[i] = i;
		if(check())return i+1;
		int tar = i;
		for(int j = 0;j<N;j++){
			if(perm[j] == i)Q[i] = j;
		}
		act(P[i],Q[i]);
		if(check())return i+1;
	}
	assert(false);
	return -1;

	/*
	for(int i = 0;i+2<N;i++){
		OP(i);
		if(check())return i;
		P[i] = pos[i+2],Q[i] = i+2;
		act(i+2,pos[i+2]);
		assert(perm[i+2] == i+2);
		if(check())return i;
	}
	OP(N-2);
	P[N-2] = Q[N-2] = 0;
	if(check())return N-1;
	else{
		P[N-2] = 0;
		Q[N-2] = 1;
		return N-1;
	}
	*/
	return -1;
}

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:58:7: warning: unused variable 'tar' [-Wunused-variable]
   58 |   int tar = i;
      |       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...