Submission #795730

# Submission time Handle Problem Language Result Execution time Memory
795730 2023-07-27T13:47:16 Z Username4132 Sorting (IOI15_sorting) C++14
100 / 100
152 ms 14208 KB
#include "sorting.h"
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using pii = pair<int, int>;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define forsn(i, s, n) for(int i=s; i<(int)n; ++i)
#define PB push_back
#define F first
#define S second

int n, cur;

vector<pii> gen(vector<int>& p){
	vector<int> q(n);
	forn(i, n) q[i]=p[i];
	vector<pii> ret;
	vector<bool> vis(n, false);
	forn(i, n) if(!vis[i]){
		vis[i]=true;
		int x=p[i];
		while(x!=i){
			vis[x]=true;
			ret.PB({q[i], q[x]});
			swap(q[i], q[x]);
			x=p[x];
		}
	}
	return ret;
}

void mov(vector<int>& per, int* X, int* Y, int pos){
	while(cur<pos){
		swap(per[X[cur]], per[Y[cur]]);
		++cur;
	}
	while(cur>pos){
		--cur;
		swap(per[X[cur]], per[Y[cur]]);
	}
}

int findSwapPairs(int N, int seq[], int M, int X[], int Y[], int P[], int Q[]) {
	n=N;
	vector<int> per(n);
	forn(i, n) per[i]=seq[i];

	mov(per, X, Y, 3);
	int lo=-1, hi=n;
	while(hi-lo>1){
		int mid = (hi+lo)/2;
		mov(per, X, Y, mid);
		vector<pii> res = gen(per);
		if(res.size()>mid) lo=mid;
		else hi=mid;
	}

	mov(per, X, Y, hi);
	vector<pii> res = gen(per);
	int ans = res.size();
	vector<int> inv(n);
	forn(i, n) inv[seq[i]]=i;

	forn(i, ans){
		int a = res[i].F, b = res[i].S;
		swap(inv[seq[X[i]]], inv[seq[Y[i]]]);
		swap(seq[X[i]], seq[Y[i]]);
		P[i]=inv[a], Q[i]=inv[b];
		swap(seq[P[i]], seq[Q[i]]);
		swap(inv[a], inv[b]);
	}
	forsn(i, ans, hi) P[i]=Q[i]=0;
	return hi;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:55:16: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |   if(res.size()>mid) lo=mid;
      |      ~~~~~~~~~~^~~~
sorting.cpp:61:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   61 |  int ans = res.size();
      |            ~~~~~~~~^~
sorting.cpp:44:41: warning: unused parameter 'M' [-Wunused-parameter]
   44 | int findSwapPairs(int N, int seq[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                     ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 304 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 304 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 0 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 296 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 436 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 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 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 432 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 436 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 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 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 432 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 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 340 KB Output is correct
15 Correct 125 ms 12996 KB Output is correct
16 Correct 139 ms 13352 KB Output is correct
17 Correct 147 ms 13584 KB Output is correct
18 Correct 56 ms 9464 KB Output is correct
19 Correct 114 ms 11252 KB Output is correct
20 Correct 113 ms 12272 KB Output is correct
21 Correct 118 ms 12296 KB Output is correct
22 Correct 120 ms 13384 KB Output is correct
23 Correct 138 ms 14084 KB Output is correct
24 Correct 144 ms 13996 KB Output is correct
25 Correct 141 ms 13752 KB Output is correct
26 Correct 113 ms 12168 KB Output is correct
27 Correct 118 ms 11276 KB Output is correct
28 Correct 143 ms 13656 KB Output is correct
29 Correct 139 ms 13424 KB Output is correct
30 Correct 76 ms 10116 KB Output is correct
31 Correct 147 ms 13576 KB Output is correct
32 Correct 152 ms 13632 KB Output is correct
33 Correct 140 ms 13980 KB Output is correct
34 Correct 141 ms 13832 KB Output is correct
35 Correct 113 ms 11288 KB Output is correct
36 Correct 50 ms 8908 KB Output is correct
37 Correct 143 ms 14208 KB Output is correct
38 Correct 134 ms 13816 KB Output is correct
39 Correct 144 ms 13944 KB Output is correct