Submission #432695

# Submission time Handle Problem Language Result Execution time Memory
432695 2021-06-18T12:27:31 Z Kevin_Zhang_TW Sorting (IOI15_sorting) C++17
100 / 100
358 ms 23780 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 600010;
#include "sorting.h"

int n, a[MAX_N], *s, *p, *q;
int *sx, *sy;

vector<pair<int,int>> count_cost(int *s) {
	vector<int> a(s, s+n);

	vector<pair<int,int>> ret;

	for (int i = 0;i < n;++i) {
		while (a[i] != i) {
			int x = i, y = a[i];
			swap(a[x], a[y]);
			ret.pb(a[x], a[y]);
		}
	} 

	return ret; 
}

int vx[MAX_N], vy[MAX_N], pos[MAX_N];

void mswap_pos(int x, int y) {
	swap(a[x], a[y]);
	swap(pos[ a[x] ], pos[ a[y] ]);
}

bool valid(int len) {
	copy(s, s+n, a);
	for (int i = 0;i < len;++i) {
		swap(a[ sx[i] ], a[ sy[i] ]);
		vx[i] = a[ sx[i] ], vy[i] = a[ sy[i] ];
	}
	auto swv = count_cost(a);
	if (swv.size() > len) return false;

	while (swv.size() < len) swv.pb(0, 0);

	copy(s, s+n, a);
	for (int i = 0;i < n;++i) pos[ a[i] ] = i;
	for (int i = 0;i < len;++i) {
		mswap_pos(sx[i], sy[i]);
		auto [x, y] = swv[i];
		p[i] = pos[x], q[i] = pos[y];
		mswap_pos( pos[x], pos[y] );

	} 

	for (int i = 0;i < n;++i) assert(i == a[i]);

	return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	p = P, q = Q, s = S, sx = X, sy = Y;
	n = N;
	for (int i = 0;i < N;++i)
		DE(sx[i], sy[i]);

	int l = 0, r = n, mid;
	while (l < r) {
		mid = l + r >> 1;
		if (valid(mid))
			r =	mid;
		else
			l = mid+1;
	}
	assert(valid(l));
	valid(l);
	DE(l);
	return l; 
}

Compilation message

sorting.cpp: In function 'std::vector<std::pair<int, int> > count_cost(int*)':
sorting.cpp:24:39: warning: declaration of 's' shadows a global declaration [-Wshadow]
   24 | vector<pair<int,int>> count_cost(int *s) {
      |                                  ~~~~~^
sorting.cpp:21:19: note: shadowed declaration is here
   21 | int n, a[MAX_N], *s, *p, *q;
      |                   ^
sorting.cpp:25:14: warning: declaration of 'a' shadows a global declaration [-Wshadow]
   25 |  vector<int> a(s, s+n);
      |              ^
sorting.cpp:21:8: note: shadowed declaration is here
   21 | int n, a[MAX_N], *s, *p, *q;
      |        ^
sorting.cpp: In function 'bool valid(int)':
sorting.cpp:54:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   54 |  if (swv.size() > len) return false;
      |      ~~~~~~~~~~~^~~~~
sorting.cpp:56:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   56 |  while (swv.size() < len) swv.pb(0, 0);
      |         ~~~~~~~~~~~^~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
sorting.cpp:76:3: note: in expansion of macro 'DE'
   76 |   DE(sx[i], sy[i]);
      |   ^~
sorting.cpp:80:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |   mid = l + r >> 1;
      |         ~~^~~
sorting.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
sorting.cpp:88:2: note: in expansion of macro 'DE'
   88 |  DE(l);
      |  ^~
sorting.cpp:72:39: warning: unused parameter 'M' [-Wunused-parameter]
   72 | 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 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 572 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 3 ms 528 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 2 ms 436 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 440 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 3 ms 528 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 2 ms 436 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 232 ms 20940 KB Output is correct
16 Correct 243 ms 21656 KB Output is correct
17 Correct 288 ms 22624 KB Output is correct
18 Correct 92 ms 17852 KB Output is correct
19 Correct 234 ms 19876 KB Output is correct
20 Correct 248 ms 22524 KB Output is correct
21 Correct 248 ms 22708 KB Output is correct
22 Correct 283 ms 18152 KB Output is correct
23 Correct 261 ms 23592 KB Output is correct
24 Correct 317 ms 23088 KB Output is correct
25 Correct 302 ms 22848 KB Output is correct
26 Correct 169 ms 21700 KB Output is correct
27 Correct 231 ms 20132 KB Output is correct
28 Correct 358 ms 22888 KB Output is correct
29 Correct 352 ms 22384 KB Output is correct
30 Correct 117 ms 19852 KB Output is correct
31 Correct 343 ms 22816 KB Output is correct
32 Correct 299 ms 22704 KB Output is correct
33 Correct 339 ms 23052 KB Output is correct
34 Correct 285 ms 23084 KB Output is correct
35 Correct 220 ms 19568 KB Output is correct
36 Correct 94 ms 19160 KB Output is correct
37 Correct 285 ms 23780 KB Output is correct
38 Correct 262 ms 22840 KB Output is correct
39 Correct 293 ms 22952 KB Output is correct