Submission #331635

# Submission time Handle Problem Language Result Execution time Memory
331635 2020-11-29T09:43:19 Z monkey8 Sorting (IOI15_sorting) C++14
0 / 100
5 ms 1388 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 = 1;
    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 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 1132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1264 KB Output is correct
2 Correct 3 ms 1388 KB Output is correct
3 Correct 5 ms 1344 KB Output is correct
4 Incorrect 2 ms 1260 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1264 KB Output is correct
2 Correct 3 ms 1388 KB Output is correct
3 Correct 5 ms 1344 KB Output is correct
4 Incorrect 2 ms 1260 KB Output isn't correct
5 Halted 0 ms 0 KB -