Submission #284980

# Submission time Handle Problem Language Result Execution time Memory
284980 2020-08-28T08:40:02 Z _7_7_ Sorting (IOI15_sorting) C++14
100 / 100
495 ms 26088 KB
#include "sorting.h"
#include <bits/stdc++.h>                                           
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;  
typedef unsigned long long ull;         
typedef vector < pii > vpii;                                   	
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;
 
typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
 
const int inf = 1e9, maxn = 2e5 + 48, mod = 998244353, N = 2e5 + 12;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}, block = 300;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = acos(-1);
const ll INF = 1e18;

vector < pii > ans;
int a[N], b[N], c[N], d[N], e[N], n, s[N], t[N], pos[N], pos1[N];

void Swap (int i, int j) { 
	swap(t[i], t[j]);
	swap(pos[t[i]], pos[t[j]]);
}

void Swap1 (int i, int j) { 
	swap(s[i], s[j]);
	swap(pos1[s[i]], pos1[s[j]]);
}


bool check (int T) {
	ans.clear();
	for (int i = 0; i < n; ++i)	{
		s[i] = a[i];             
		t[i] = a[i];
		pos[t[i]] = pos1[s[i]] = i;
	}

	for (int i = 0; i < T; ++i) 
		Swap1(c[i], b[i]);
	int j = 0;

	for (int i = 0; i < T; ++i) {
		Swap(c[i], b[i]);
		while (j < n && s[j] == j)
			++j;

		if (j < n) {
			ans.pb(mp(pos[j], pos[s[j]]));
			Swap(pos[j], pos[s[j]]);
			Swap1(j, pos1[j]); 
		} else
			ans.pb(mp(0, 0));
	}

	while (j < n && s[j] == j)
		++j;

	return (j == n);			
}

int findSwapPairs(int _n, int s[], int m, int x[], int y[], int p[], int q[]) {
	n = _n;
	for (int i = 0; i < n; ++i) {
		a[i] = s[i];	          
		b[i] = x[i];
		c[i] = y[i];
	}

	int l = 0, r = n - 1, res = n;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) {
			r = mid - 1;
			res = mid;
		} else
			l = mid + 1;
	}


	check(res);
	for (int i = 0; i < sz(ans); ++i) {
		p[i] = ans[i].f;                
		q[i] = ans[i].s;
	}

	for (int i = sz(ans); i < res; ++i)
		p[i] = q[i] = 0;

	

	return res;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:90:77: warning: declaration of 'second' shadows a global declaration [-Wshadow]
   90 | int findSwapPairs(int _n, int s[], int m, int x[], int y[], int p[], int q[]) {
      |                                                                             ^
sorting.cpp:22:11: note: shadowed declaration is here
   22 | #define s second
      |           ^~~~~~
sorting.cpp:46:38: note: in expansion of macro 's'
   46 | int a[N], b[N], c[N], d[N], e[N], n, s[N], t[N], pos[N], pos1[N];
      |                                      ^
sorting.cpp:90:40: warning: unused parameter 'm' [-Wunused-parameter]
   90 | int findSwapPairs(int _n, int s[], int m, int x[], int y[], int p[], int q[]) {
      |                                    ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 320 ms 15264 KB Output is correct
16 Correct 390 ms 23528 KB Output is correct
17 Correct 491 ms 24808 KB Output is correct
18 Correct 93 ms 19696 KB Output is correct
19 Correct 292 ms 22508 KB Output is correct
20 Correct 351 ms 23684 KB Output is correct
21 Correct 344 ms 23656 KB Output is correct
22 Correct 324 ms 20200 KB Output is correct
23 Correct 336 ms 25704 KB Output is correct
24 Correct 490 ms 25320 KB Output is correct
25 Correct 478 ms 25028 KB Output is correct
26 Correct 297 ms 23276 KB Output is correct
27 Correct 275 ms 22520 KB Output is correct
28 Correct 474 ms 25064 KB Output is correct
29 Correct 410 ms 24424 KB Output is correct
30 Correct 189 ms 21868 KB Output is correct
31 Correct 434 ms 24936 KB Output is correct
32 Correct 361 ms 24808 KB Output is correct
33 Correct 489 ms 25320 KB Output is correct
34 Correct 437 ms 25176 KB Output is correct
35 Correct 277 ms 22172 KB Output is correct
36 Correct 65 ms 20976 KB Output is correct
37 Correct 495 ms 26088 KB Output is correct
38 Correct 459 ms 25064 KB Output is correct
39 Correct 491 ms 25192 KB Output is correct