Submission #587513

# Submission time Handle Problem Language Result Execution time Memory
587513 2022-07-02T03:56:53 Z benson1029 Sorting (IOI15_sorting) C++14
74 / 100
1000 ms 15048 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int a[200005], b[200005], s[200005];
vector< pair<int,int> > v;

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]]);
		}
		v.clear();

		for(int i=0; i<mid; i++) {
			swap(a[X[i]], a[Y[i]]);
			swap(s[X[i]], s[Y[i]]);
			for(int j=0; j<N; j++) {
				b[a[j]] = j;
			}
			for(int j=0; j<N; j++) {
				if(s[j] == a[j]) continue;
				v.push_back({j, b[s[j]]});
				swap(s[j], s[b[s[j]]]);
				goto end;
			}
			v.push_back({0, 0});
			end:;
		}

		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 {
			bool ok = true;
			for(int i=0; i<N; i++) if(s[i] != i) ok = false;

			if(ok) r = mid;
			else l = mid+1;
		}
		
	}

}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:40: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]
   40 |    for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
      |                 ~^~~~~~~~~
sorting.cpp:41:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   41 |    return v.size();
      |           ~~~~~~^~
sorting.cpp:8:39: warning: unused parameter 'M' [-Wunused-parameter]
    8 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:52:1: warning: control reaches end of non-void function [-Wreturn-type]
   52 | }
      | ^
# Verdict Execution time Memory 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 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 220 KB Output is correct
# Verdict Execution time Memory 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 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 220 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 220 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 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 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 212 KB Output is correct
21 Correct 6 ms 468 KB Output is correct
22 Correct 7 ms 548 KB Output is correct
23 Correct 7 ms 468 KB Output is correct
24 Correct 6 ms 564 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 5 ms 572 KB Output is correct
27 Correct 6 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 468 KB Output is correct
2 Correct 68 ms 500 KB Output is correct
3 Correct 95 ms 472 KB Output is correct
4 Correct 11 ms 476 KB Output is correct
5 Correct 17 ms 468 KB Output is correct
6 Correct 28 ms 452 KB Output is correct
7 Correct 72 ms 472 KB Output is correct
8 Correct 102 ms 468 KB Output is correct
9 Correct 89 ms 480 KB Output is correct
10 Correct 107 ms 476 KB Output is correct
11 Correct 99 ms 476 KB Output is correct
12 Correct 71 ms 468 KB Output is correct
13 Correct 101 ms 468 KB Output is correct
14 Correct 12 ms 476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 468 KB Output is correct
2 Correct 68 ms 500 KB Output is correct
3 Correct 95 ms 472 KB Output is correct
4 Correct 11 ms 476 KB Output is correct
5 Correct 17 ms 468 KB Output is correct
6 Correct 28 ms 452 KB Output is correct
7 Correct 72 ms 472 KB Output is correct
8 Correct 102 ms 468 KB Output is correct
9 Correct 89 ms 480 KB Output is correct
10 Correct 107 ms 476 KB Output is correct
11 Correct 99 ms 476 KB Output is correct
12 Correct 71 ms 468 KB Output is correct
13 Correct 101 ms 468 KB Output is correct
14 Correct 12 ms 476 KB Output is correct
15 Execution timed out 1051 ms 15048 KB Time limit exceeded
16 Halted 0 ms 0 KB -