Submission #763565

# Submission time Handle Problem Language Result Execution time Memory
763565 2023-06-22T13:00:45 Z Ahmed57 Sorting (IOI15_sorting) C++17
100 / 100
188 ms 26968 KB
#include <bits/stdc++.h>

using namespace std;
int vis[200001];
vector<int> vs;
vector<int> now;
void dfs(int i){
    vis[i] = 1;
    vs.push_back(i);
    if(!vis[now[i]])dfs(now[i]);
}
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
    long long l = 0 , r = M , nah = 0;
    while(l<=r){
        long long mid = (l+r)/2;
        now.clear();
        for(int i = 0;i<N;i++)now.push_back(S[i]);
        for(int i = 0;i<mid;i++){
            swap(now[X[i]],now[Y[i]]);
        }
        memset(vis,0,sizeof vis);
        int ans = N;
        for(int i = 0;i<N;i++){
            if(!vis[i]){
                vs.clear();
                dfs(i);
                ans--;
            }
        }
        if(ans<=mid){
            r = mid-1;
            nah = mid;
        }else l = mid+1;
    }
    now.clear();
    for(int i = 0;i<N;i++)now.push_back(S[i]);
    for(int i = 0;i<nah;i++){
        swap(now[X[i]],now[Y[i]]);
    }
    memset(vis,0,sizeof vis);
    vector<pair<int,int>> swps;
    for(int i = 0;i<N;i++){
        if(!vis[i]){
            vs.clear();
            dfs(i);
            for(int xd = 1;xd<vs.size();xd++){
                swps.push_back({vs[0],vs[xd]});
            }
        }
    }
    reverse(swps.begin(),swps.end());
    int pos[N] = {0};
    now.clear();
    for(int i = 0;i<N;i++){
        now.push_back(S[i]);pos[S[i]] = i;
    }
    for(int i = 0;i<nah;i++){
        swap(S[X[i]],S[Y[i]]);
        swap(pos[S[X[i]]],pos[S[Y[i]]]);
        if(swps.empty()){
            P[i] = 0;
            Q[i] = 0;
        }else{
            P[i] = pos[swps.back().first];
            Q[i] = pos[swps.back().second];
            swps.pop_back();
        }
        //cout<<X[i]<<" "<<Y[i]<<" "<<P[i]<<" "<<Q[i]<<endl;
    }
    return nah;
}/*
int main(){
    int S[] ={3,0,4,2,1};
    int X[] = {1,4,2,1,0};
    int Y[] ={1,0,3,4,4};
    int P[] ={0,0,0,0,0};
    int Q[] = {0,0,0,0,0};
    findSwapPairs(5,S,5,X,Y,P,Q);
}
*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:46:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |             for(int xd = 1;xd<vs.size();xd++){
      |                            ~~^~~~~~~~~~
sorting.cpp:70:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   70 |     return nah;
      |            ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1080 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1080 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1080 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1084 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1076 KB Output is correct
19 Correct 1 ms 980 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 2 ms 1236 KB Output is correct
22 Correct 2 ms 1236 KB Output is correct
23 Correct 2 ms 1236 KB Output is correct
24 Correct 2 ms 1236 KB Output is correct
25 Correct 1 ms 1236 KB Output is correct
26 Correct 2 ms 1216 KB Output is correct
27 Correct 2 ms 1348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1236 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 2 ms 1236 KB Output is correct
5 Correct 2 ms 1284 KB Output is correct
6 Correct 2 ms 1220 KB Output is correct
7 Correct 2 ms 1220 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1224 KB Output is correct
12 Correct 2 ms 1220 KB Output is correct
13 Correct 2 ms 1216 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1236 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 2 ms 1236 KB Output is correct
5 Correct 2 ms 1284 KB Output is correct
6 Correct 2 ms 1220 KB Output is correct
7 Correct 2 ms 1220 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1224 KB Output is correct
12 Correct 2 ms 1220 KB Output is correct
13 Correct 2 ms 1216 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 150 ms 23288 KB Output is correct
16 Correct 145 ms 24236 KB Output is correct
17 Correct 159 ms 25952 KB Output is correct
18 Correct 73 ms 18984 KB Output is correct
19 Correct 132 ms 22824 KB Output is correct
20 Correct 149 ms 21904 KB Output is correct
21 Correct 140 ms 23808 KB Output is correct
22 Correct 144 ms 18296 KB Output is correct
23 Correct 159 ms 26968 KB Output is correct
24 Correct 173 ms 26488 KB Output is correct
25 Correct 170 ms 25840 KB Output is correct
26 Correct 137 ms 19192 KB Output is correct
27 Correct 129 ms 18680 KB Output is correct
28 Correct 167 ms 25372 KB Output is correct
29 Correct 147 ms 20028 KB Output is correct
30 Correct 104 ms 17916 KB Output is correct
31 Correct 148 ms 20300 KB Output is correct
32 Correct 175 ms 25880 KB Output is correct
33 Correct 158 ms 26208 KB Output is correct
34 Correct 157 ms 20744 KB Output is correct
35 Correct 130 ms 21972 KB Output is correct
36 Correct 59 ms 16804 KB Output is correct
37 Correct 188 ms 26776 KB Output is correct
38 Correct 173 ms 26368 KB Output is correct
39 Correct 166 ms 25712 KB Output is correct