Submission #707035

# Submission time Handle Problem Language Result Execution time Memory
707035 2023-03-08T10:12:54 Z marvinthang Sorting (IOI15_sorting) C++17
100 / 100
204 ms 21268 KB
/*************************************
*    author: marvinthang             *
*    created: 08.03.2023 16:53:13    *
*************************************/

#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> scan_op(pair <U, V>)  { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }

// end of template

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    auto check = [&] (int r, bool trace = false) {
    	vector <int> perm(S, S + N);
    	REP(i, r) swap(perm[X[i]], perm[Y[i]]);
    	vector <int> pos(N);
    	REP(i, N) pos[perm[i]] = i;
    	vector <pair <int, int>> swaps;
    	auto __swap = [&] (int i, int j) {
			swap(perm[i], perm[j]);
			swap(pos[perm[i]], pos[perm[j]]);    		
    	};
    	REP(i, N) {
    		if (perm[i] != i) {
    			swaps.push_back(make_pair(perm[i], i));
    			__swap(i, pos[i]);
    		}
    	}
    	if (!trace) return swaps.size() <= r;
    	while (swaps.size() < r) swaps.push_back(make_pair(0, 0));
    	copy(S, S + N, perm.begin());
    	REP(i, N) pos[perm[i]] = i;
    	REP(i, r) {
    		__swap(X[i], Y[i]);
    		P[i] = pos[swaps[i].fi];
    		Q[i] = pos[swaps[i].se];
    		__swap(P[i], Q[i]);
    	}
    	return true;
    };
    int l = 0, r = N;
    while (l < r) {
    	int m = l + r >> 1;
    	if (check(m)) r = m;
    	else l = m + 1;
    }
    check(l, true);
    return l;
}

#ifdef LOCAL 
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;

static inline int _read() {
    if (_charsNumber < 0) {
        exit(1);
    }
    if (!_charsNumber || _currentChar == _charsNumber) {
        _charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
        _currentChar = 0;
    }
    if (_charsNumber <= 0) {
        return -1;
    }
    return _buffer[_currentChar++];
}

static inline int _readInt() {
    int c, x, s;
    c = _read();
    while (c <= 32) c = _read();
    x = 0;
    s = 1;
    if (c == '-') {
        s = -1;
        c = _read();
    }
    while (c > 32) {
        x *= 10;
        x += c - '0';
        c = _read();
    }
    if (s < 0) x = -x;
    return x;
}

int main()
{
	_inputFile = fopen("sorting.in", "rb");
	_outputFile = fopen("sorting.out", "w");
	int N, M;
	N = _readInt();
	int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
	int *perm = (int*)malloc(sizeof(int) * (unsigned int)N);
	for (int i = 0; i < N; ++i)
		perm[i] = S[i] = _readInt();
	M = _readInt();
	int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
	for (int i = 0; i < M; ++i) {
	    X[i] = _readInt();
	    Y[i] = _readInt();
	}
	int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
	int ans = findSwapPairs(N, S, M, X, Y, P, Q);
	fprintf(_outputFile, "%d\n", ans);
	for (int i = 0; i < ans; ++i) {
		swap(perm[X[i]], perm[Y[i]]);
		swap(perm[P[i]], perm[Q[i]]);
		fprintf(_outputFile, "%d %d\n", P[i], Q[i]);
	}
	REP(i, N) fprintf(_outputFile, "%d ", perm[i]);
	return 0;
}
#endif

Compilation message

sorting.cpp: In lambda function:
sorting.cpp:63:38: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   63 |      if (!trace) return swaps.size() <= r;
      |                         ~~~~~~~~~~~~~^~~~
sorting.cpp:64:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |      while (swaps.size() < r) swaps.push_back(make_pair(0, 0));
      |             ~~~~~~~~~~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:77:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   77 |      int m = l + r >> 1;
      |              ~~^~~
sorting.cpp:46:39: warning: unused parameter 'M' [-Wunused-parameter]
   46 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 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 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 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 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 308 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 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 0 ms 304 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 456 KB Output is correct
22 Correct 1 ms 424 KB Output is correct
23 Correct 1 ms 556 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 440 KB Output is correct
27 Correct 1 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 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 484 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 2 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 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 484 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 2 ms 440 KB Output is correct
15 Correct 169 ms 18984 KB Output is correct
16 Correct 175 ms 19428 KB Output is correct
17 Correct 199 ms 20412 KB Output is correct
18 Correct 59 ms 15964 KB Output is correct
19 Correct 141 ms 18184 KB Output is correct
20 Correct 140 ms 20276 KB Output is correct
21 Correct 149 ms 20244 KB Output is correct
22 Correct 162 ms 15632 KB Output is correct
23 Correct 188 ms 20960 KB Output is correct
24 Correct 201 ms 20768 KB Output is correct
25 Correct 196 ms 20448 KB Output is correct
26 Correct 132 ms 20196 KB Output is correct
27 Correct 133 ms 18188 KB Output is correct
28 Correct 204 ms 20660 KB Output is correct
29 Correct 186 ms 20308 KB Output is correct
30 Correct 96 ms 18064 KB Output is correct
31 Correct 178 ms 20756 KB Output is correct
32 Correct 185 ms 20316 KB Output is correct
33 Correct 201 ms 20648 KB Output is correct
34 Correct 178 ms 20580 KB Output is correct
35 Correct 136 ms 17964 KB Output is correct
36 Correct 60 ms 15872 KB Output is correct
37 Correct 197 ms 21268 KB Output is correct
38 Correct 191 ms 20640 KB Output is correct
39 Correct 188 ms 20708 KB Output is correct