Submission #331636

# Submission time Handle Problem Language Result Execution time Memory
331636 2020-11-29T09:51:01 Z monkey8 Sorting (IOI15_sorting) C++14
100 / 100
212 ms 20944 KB
#include "sorting.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cstdio>
#include <utility> 
#include <queue>
#include <math.h>
#include <set>
#include <bitset>
#include <cmath>
#include <bitset>
#include <iterator>
#include <limits>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 200010;
int visited[MAXN];
int vals[MAXN];
int idxs[MAXN];
void dfs(int u) {
    visited[u] = 1;
    if(!visited[vals[u]]) dfs(vals[u]);
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    int low = 0;
    int high = n;
    while(low < high) {
        int avg = (low + high) / 2;
        for(int i = 0; i < n; i++)
            vals[i] = s[i];
        for(int i = 0; i < avg; i++) {
            int temp = vals[x[i]];
            vals[x[i]] = vals[y[i]];
            vals[y[i]] = temp;
        }
        int num = 0;
        for(int i = 0; i < n; i++) {
            if(!visited[i]) {
                dfs(i);
                num++;
            }
        }
        memset(visited, 0, sizeof(visited));
        if(n - num <= avg) high = avg;
        else low = avg + 1;
    }
    for(int i = 0; i < n; i++) {
        vals[i] = s[i];
        idxs[s[i]] = i;
    }
    for(int i = 0; i < low; i++) {
        int temp = vals[x[i]];
        vals[x[i]] = vals[y[i]];
        vals[y[i]] = temp;
    }
    vector<pii> swaps;
    for(int i = 0; i < n; i++) {
        if(i == vals[i]) continue;
        int j = i;
        visited[j] = 1;
        while(!visited[vals[j]]) {
            swaps.push_back(pii(j, vals[j]));
            visited[vals[j]] = 1;
            j = vals[j];
        }
    }
    for(int i = 0; i < n; i++) {
        vals[i] = s[i];
        idxs[s[i]] = i;
    }
    for(int i = 0; i < low; i++) {
        int temp = vals[x[i]];
        idxs[vals[x[i]]] = y[i];
        idxs[vals[y[i]]] = x[i];
        vals[x[i]] = vals[y[i]];
        vals[y[i]] = temp;
        if(i < (int)swaps.size()) {
            p[i] = idxs[swaps[i].first];
            q[i] = idxs[swaps[i].second];
            temp = idxs[swaps[i].first];
            idxs[swaps[i].first] = idxs[swaps[i].second];
            idxs[swaps[i].second] = temp;
            temp = vals[p[i]];
            vals[p[i]] = vals[q[i]];
            vals[q[i]] = temp;
        }
        else {
            p[i] = 0;
            q[i] = 0;
        }
    }
    return low;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:29:39: warning: unused parameter 'm' [-Wunused-parameter]
   29 | 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 1132 KB Output is correct
2 Correct 1 ms 1132 KB Output is correct
3 Correct 1 ms 1132 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1132 KB Output is correct
6 Correct 1 ms 1132 KB Output is correct
7 Correct 1 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1132 KB Output is correct
2 Correct 1 ms 1132 KB Output is correct
3 Correct 1 ms 1132 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1132 KB Output is correct
6 Correct 1 ms 1132 KB Output is correct
7 Correct 1 ms 1132 KB Output is correct
8 Correct 1 ms 1132 KB Output is correct
9 Correct 1 ms 1132 KB Output is correct
10 Correct 2 ms 1132 KB Output is correct
11 Correct 1 ms 1132 KB Output is correct
12 Correct 1 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1132 KB Output is correct
2 Correct 1 ms 1132 KB Output is correct
3 Correct 1 ms 1132 KB Output is correct
4 Correct 1 ms 1132 KB Output is correct
5 Correct 1 ms 1132 KB Output is correct
6 Correct 1 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1132 KB Output is correct
2 Correct 1 ms 1132 KB Output is correct
3 Correct 1 ms 1132 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1132 KB Output is correct
6 Correct 1 ms 1132 KB Output is correct
7 Correct 1 ms 1132 KB Output is correct
8 Correct 1 ms 1132 KB Output is correct
9 Correct 1 ms 1132 KB Output is correct
10 Correct 2 ms 1132 KB Output is correct
11 Correct 1 ms 1132 KB Output is correct
12 Correct 1 ms 1132 KB Output is correct
13 Correct 1 ms 1132 KB Output is correct
14 Correct 1 ms 1132 KB Output is correct
15 Correct 1 ms 1132 KB Output is correct
16 Correct 1 ms 1132 KB Output is correct
17 Correct 1 ms 1132 KB Output is correct
18 Correct 1 ms 1132 KB Output is correct
19 Correct 1 ms 1132 KB Output is correct
20 Correct 1 ms 1132 KB Output is correct
21 Correct 2 ms 1388 KB Output is correct
22 Correct 2 ms 1408 KB Output is correct
23 Correct 3 ms 1388 KB Output is correct
24 Correct 2 ms 1408 KB Output is correct
25 Correct 2 ms 1388 KB Output is correct
26 Correct 2 ms 1400 KB Output is correct
27 Correct 2 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 2 ms 1260 KB Output is correct
3 Correct 2 ms 1260 KB Output is correct
4 Correct 2 ms 1132 KB Output is correct
5 Correct 2 ms 1260 KB Output is correct
6 Correct 2 ms 1260 KB Output is correct
7 Correct 2 ms 1260 KB Output is correct
8 Correct 2 ms 1260 KB Output is correct
9 Correct 3 ms 1388 KB Output is correct
10 Correct 2 ms 1260 KB Output is correct
11 Correct 3 ms 1408 KB Output is correct
12 Correct 2 ms 1260 KB Output is correct
13 Correct 2 ms 1260 KB Output is correct
14 Correct 2 ms 1260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 2 ms 1260 KB Output is correct
3 Correct 2 ms 1260 KB Output is correct
4 Correct 2 ms 1132 KB Output is correct
5 Correct 2 ms 1260 KB Output is correct
6 Correct 2 ms 1260 KB Output is correct
7 Correct 2 ms 1260 KB Output is correct
8 Correct 2 ms 1260 KB Output is correct
9 Correct 3 ms 1388 KB Output is correct
10 Correct 2 ms 1260 KB Output is correct
11 Correct 3 ms 1408 KB Output is correct
12 Correct 2 ms 1260 KB Output is correct
13 Correct 2 ms 1260 KB Output is correct
14 Correct 2 ms 1260 KB Output is correct
15 Correct 191 ms 18796 KB Output is correct
16 Correct 175 ms 19280 KB Output is correct
17 Correct 183 ms 20248 KB Output is correct
18 Correct 63 ms 15596 KB Output is correct
19 Correct 149 ms 18592 KB Output is correct
20 Correct 164 ms 19276 KB Output is correct
21 Correct 163 ms 19128 KB Output is correct
22 Correct 172 ms 15724 KB Output is correct
23 Correct 175 ms 20944 KB Output is correct
24 Correct 204 ms 20580 KB Output is correct
25 Correct 198 ms 20472 KB Output is correct
26 Correct 153 ms 19280 KB Output is correct
27 Correct 147 ms 18600 KB Output is correct
28 Correct 183 ms 20548 KB Output is correct
29 Correct 212 ms 19916 KB Output is correct
30 Correct 106 ms 17888 KB Output is correct
31 Correct 181 ms 20396 KB Output is correct
32 Correct 188 ms 20096 KB Output is correct
33 Correct 202 ms 20868 KB Output is correct
34 Correct 187 ms 20596 KB Output is correct
35 Correct 149 ms 18548 KB Output is correct
36 Correct 56 ms 16748 KB Output is correct
37 Correct 196 ms 20664 KB Output is correct
38 Correct 188 ms 20472 KB Output is correct
39 Correct 189 ms 19820 KB Output is correct