Submission #587517

# Submission time Handle Problem Language Result Execution time Memory
587517 2022-07-02T04:16:16 Z benson1029 Sorting (IOI15_sorting) C++14
74 / 100
1000 ms 18596 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;
set<int> st;

void swapa(int x, int y) {
	b[a[x]] = y;
	b[a[y]] = x;
	if(!stexists[x] && stexists[y]) {
		st.erase(y);
		st.insert(x);
	}
	else if(stexists[x] && !stexists[y]) {
		st.erase(x);
		st.insert(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]]);
		}
		st.clear();
		for(int i=0; i<N; i++) {
			stexists[i] = (a[i] != s[i]);
			if(a[i] != s[i]) st.insert(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]]);
			if(!st.empty()) {
				int j = *st.begin();
				int k = b[s[j]];
				swap(s[j], s[k]);
				v.push_back({j, k});
				if(s[j] == a[j]) {
					st.erase(j);
					stexists[j] = false;
				}
				if(s[k] == a[k]) {
					st.erase(k);
					stexists[k] = false;
				}
			} else v.push_back({0, 0});
		}

		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 {
			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:67: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]
   67 |    for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
      |                 ~^~~~~~~~~
sorting.cpp:68:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   68 |    return v.size();
      |           ~~~~~~^~
sorting.cpp:26:39: warning: unused parameter 'M' [-Wunused-parameter]
   26 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:74:1: warning: control reaches end of non-void function [-Wreturn-type]
   74 | }
      | ^
# 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 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 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 312 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 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 312 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 1 ms 304 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 312 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 1 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 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 312 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 304 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 312 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 3 ms 468 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 9 ms 596 KB Output is correct
3 Correct 8 ms 504 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 5 ms 468 KB Output is correct
8 Correct 9 ms 580 KB Output is correct
9 Correct 7 ms 596 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 8 ms 596 KB Output is correct
12 Correct 5 ms 576 KB Output is correct
13 Correct 6 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 468 KB Output is correct
2 Correct 9 ms 596 KB Output is correct
3 Correct 8 ms 504 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 5 ms 468 KB Output is correct
8 Correct 9 ms 580 KB Output is correct
9 Correct 7 ms 596 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 8 ms 596 KB Output is correct
12 Correct 5 ms 576 KB Output is correct
13 Correct 6 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Execution timed out 1043 ms 18596 KB Time limit exceeded
16 Halted 0 ms 0 KB -