Submission #277653

# Submission time Handle Problem Language Result Execution time Memory
277653 2020-08-21T06:35:35 Z Mercenary Sorting (IOI15_sorting) C++14
100 / 100
595 ms 22228 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef pair<int,int> ii;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int l = 0 , h = M;
    auto check = [&](int mid){
        vector<int> cur(N , 0) , pos(N , 0);
        for(int i = 0 ; i < N ; ++i)cur[i] = S[i] , pos[cur[i]] = i;
        for(int i = 0 ; i < mid ; ++i){
            swap(pos[cur[X[i]]] , pos[cur[Y[i]]]);
            swap(cur[X[i]] , cur[Y[i]]);
        }
        vector<ii> ans;
        for(int i = 0 ; i < N ; ++i){
            if(cur[i] != i){
                ans.pb(mp(i , cur[i]));
                int tmp = pos[i];
                swap(pos[i] , pos[cur[i]]);
                swap(cur[i] , cur[tmp]);
                assert(cur[i] == i);
                assert(pos[i] == i);
            }
        }
        for(int i = 0 ; i < N ; ++i)cur[i] = S[i] , pos[cur[i]] = i;
        for(int i = 0 ; i < mid ; ++i){
            P[i] = Q[i] = 0;
            assert(pos[cur[X[i]]] == X[i]);
            assert(pos[cur[Y[i]]] == Y[i]);
            swap(pos[cur[X[i]]] , pos[cur[Y[i]]]);
            swap(cur[X[i]] , cur[Y[i]]);
            if(i < ans.size()){
                P[i] = pos[ans[i].first];
                Q[i] = pos[ans[i].second];
                swap(cur[P[i]] , cur[Q[i]]);
                swap(pos[ans[i].first] , pos[ans[i].second]);
            }
        }
        return ans.size() <= mid;
    };
    while(l <= h){
        int mid = l + h >> 1;
        if(check(mid))h = mid - 1;
        else l = mid + 1;
//        cout << mid << endl;
//        for(int i = 0 ; i < N ; ++i)cout << cur[i] << " ";
//        cout << endl;
    }
    check(l);
    return l;
}

#ifdef LOCAL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sorting.h"

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);
	for (int i = 0; i < N; ++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)
		fprintf(_outputFile, "%d %d\n", P[i], Q[i]);
	return 0;
}
#endif // LOCAL

Compilation message

sorting.cpp: In lambda function:
sorting.cpp:35: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]
   35 |             if(i < ans.size()){
      |                ~~^~~~~~~~~~~~
sorting.cpp:42:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |         return ans.size() <= mid;
      |                ~~~~~~~~~~~^~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:45:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |         int mid = l + h >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 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 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 2 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 636 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 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 3 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 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 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 354 ms 19800 KB Output is correct
16 Correct 458 ms 20220 KB Output is correct
17 Correct 557 ms 21476 KB Output is correct
18 Correct 129 ms 20164 KB Output is correct
19 Correct 297 ms 21320 KB Output is correct
20 Correct 333 ms 21908 KB Output is correct
21 Correct 328 ms 21776 KB Output is correct
22 Correct 397 ms 16576 KB Output is correct
23 Correct 429 ms 21952 KB Output is correct
24 Correct 523 ms 21692 KB Output is correct
25 Correct 595 ms 21400 KB Output is correct
26 Correct 351 ms 20572 KB Output is correct
27 Correct 310 ms 20100 KB Output is correct
28 Correct 493 ms 21768 KB Output is correct
29 Correct 469 ms 21340 KB Output is correct
30 Correct 237 ms 19448 KB Output is correct
31 Correct 498 ms 21784 KB Output is correct
32 Correct 400 ms 21508 KB Output is correct
33 Correct 564 ms 21772 KB Output is correct
34 Correct 464 ms 21696 KB Output is correct
35 Correct 308 ms 20988 KB Output is correct
36 Correct 104 ms 18424 KB Output is correct
37 Correct 526 ms 22228 KB Output is correct
38 Correct 462 ms 21400 KB Output is correct
39 Correct 519 ms 21532 KB Output is correct