Submission #397091

# Submission time Handle Problem Language Result Execution time Memory
397091 2021-05-01T11:55:15 Z Andyvanh1 Sorting (IOI15_sorting) C++14
100 / 100
209 ms 21256 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>

using namespace std;

#define pb push_back
#define INF INT32_MAX
#define vt vector


typedef vt<int> vi;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tiii;
typedef long long ll;





#define MOD  1000000007

bool works(int n, int s[], int m, int x[],int y[], int val){
    for(int i = 0; i < val; i++){
        swap(s[x[i]],s[y[i]]);
    }
    vt<bool> visited(n);
    for(int i = 0; i < n; i++){
        visited[i] = false;
    }

    int ans = 0;
    for(int i = 0; i < n; i++){
        if(!visited[i]){
            int j = s[i];
            visited[i] = true;
            while(j!=i){
                ans++;
                visited[j] = true;
                j = s[j];

            }
        }

    }
    for(int i = val-1; i >= 0; i--){
        swap(s[x[i]],s[y[i]]);
    }

    return val>=ans;
}


int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]){
    int l = 0, r= m-1;
    while(l!=r){
        int mid = (l+r)>>1;

        if(works(n,s,m,x,y,mid)){
            r = mid;
        }else{
            l = mid+1;
        }
    }
    int op[r];
    int oq[r];
    
    for(int i = 0; i < r; i++){
        swap(s[x[i]],s[y[i]]);
    }
    int index = 0;
    vt<bool> visited(n);
    for(int i = 0; i < n; i++){
        visited[i] = false;
    }

    for(int i = 0; i < n; i++){
        if(!visited[i]){
            int j = i;
            visited[i] = true;
            vi curop;
            vi curoq;
            while(s[j]!=i){

                visited[s[j]] = true;
                curop.pb(j);
                j = s[j];
                curoq.pb(j);

            }
            for(int i = index; i < index+curop.size(); i++){
               op[i] = curop[index+curop.size()-1-i];
                oq[i] = curoq[index+curop.size()-1-i];
            }
            index+=curop.size();
        }

    }
    for(int i = index; i < r; i++){
        op[i]=1;

        oq[i]=1;
    }


    for(int i = r-1; i >= 0; i--){
        swap(s[x[i]],s[y[i]]);
    }
    int revs[n];
    for(int i = 0; i < n; i++){
        revs[s[i]] = i;
    }
    for(int i = 0; i < r; i++){
        swap(revs[s[x[i]]],revs[s[y[i]]]);
        swap(s[x[i]],s[y[i]]);
        p[i] = revs[op[i]];
        q[i] = revs[oq[i]];
    }

    return r;
}

Compilation message

sorting.cpp: In function 'bool works(int, int*, int, int*, int*, int)':
sorting.cpp:28:32: warning: unused parameter 'm' [-Wunused-parameter]
   28 | bool works(int n, int s[], int m, int x[],int y[], int val){
      |                            ~~~~^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:96:21: warning: declaration of 'i' shadows a previous local [-Wshadow]
   96 |             for(int i = index; i < index+curop.size(); i++){
      |                     ^
sorting.cpp:82:13: note: shadowed declaration is here
   82 |     for(int i = 0; i < n; i++){
      |             ^
sorting.cpp:96:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |             for(int i = index; i < index+curop.size(); i++){
      |                                ~~^~~~~~~~~~~~~~~~~~~~
sorting.cpp:100:31: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  100 |             index+=curop.size();
      |                               ^
# 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 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 1 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 292 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 300 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 336 KB Output is correct
6 Correct 1 ms 284 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 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 292 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 300 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 336 KB Output is correct
18 Correct 1 ms 284 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 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 432 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 436 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 376 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 452 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 1 ms 332 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 376 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 452 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 3 ms 460 KB Output is correct
11 Correct 3 ms 460 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 162 ms 18652 KB Output is correct
16 Correct 182 ms 19172 KB Output is correct
17 Correct 194 ms 20620 KB Output is correct
18 Correct 78 ms 14044 KB Output is correct
19 Correct 157 ms 17484 KB Output is correct
20 Correct 164 ms 18244 KB Output is correct
21 Correct 163 ms 18268 KB Output is correct
22 Correct 154 ms 15932 KB Output is correct
23 Correct 200 ms 20936 KB Output is correct
24 Correct 195 ms 21256 KB Output is correct
25 Correct 195 ms 20860 KB Output is correct
26 Correct 165 ms 18264 KB Output is correct
27 Correct 150 ms 17476 KB Output is correct
28 Correct 197 ms 20216 KB Output is correct
29 Correct 197 ms 19556 KB Output is correct
30 Correct 121 ms 16552 KB Output is correct
31 Correct 207 ms 19932 KB Output is correct
32 Correct 188 ms 20692 KB Output is correct
33 Correct 204 ms 20484 KB Output is correct
34 Correct 194 ms 20420 KB Output is correct
35 Correct 172 ms 17456 KB Output is correct
36 Correct 72 ms 15140 KB Output is correct
37 Correct 209 ms 21180 KB Output is correct
38 Correct 196 ms 20260 KB Output is correct
39 Correct 204 ms 20408 KB Output is correct