답안 #448553

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
448553 2021-07-30T15:12:37 Z FEDIKUS 정렬하기 (IOI15_sorting) C++17
100 / 100
200 ms 22568 KB
#include "sorting.h"

#include<bits/stdc++.h>
#define xx first
#define yy second

using namespace std;

typedef pair<int,int> pii;

const int maxn=2e5+10;

int novi[maxn];
bool bio[maxn];

bool moze(int mid,int n,int m,int* s,int* x,int* y){
    for(int i=0;i<n;i++) novi[i]=s[i];
    for(int i=0;i<mid;i++){
        swap(novi[x[i]],novi[y[i]]);
    }
    int klk=n;
    for(int i=0;i<n;i++) bio[i]=false;
    for(int i=0;i<n;i++){
        if(bio[i]) continue;
        klk--;
        int tren=novi[i];
        while(tren!=i){
            bio[tren]=true;
            tren=novi[tren];
        }
    }
    return klk<=mid;
}

void swapovi(vector<int> niz,vector<pii> &svi){
    for(int i=0;i<niz.size()-1;i++){
        svi.push_back(make_pair(niz[i],niz[i+1]));
    }
}

int gde[maxn];

vector<pii> popravi(int n,int naj,int* s,int* x,int* y,vector<pii> svi){
    for(int i=0;i<n;i++) novi[i]=s[i];
    for(int i=0;i<n;i++) gde[novi[i]]=i;
    vector<pii> ret;
    for(int i=0;i<naj;i++){
        swap(novi[x[i]],novi[y[i]]);
        gde[novi[x[i]]]=x[i];
        gde[novi[y[i]]]=y[i];
        if(i<svi.size()){
            ret.push_back(make_pair(gde[svi[i].xx],gde[svi[i].yy]));
            int prvi=gde[svi[i].xx];
            int drugi=gde[svi[i].yy];
            swap(novi[prvi],novi[drugi]);
            gde[novi[prvi]]=prvi;
            gde[novi[drugi]]=drugi;
        }else ret.push_back(make_pair(0,0));
    }
    return ret;
}

void nadji(int naj,int n,int m,int* s,int* x,int* y,int* p,int* q){
    for(int i=0;i<n;i++) novi[i]=s[i];
    for(int i=0;i<naj;i++) swap(novi[x[i]],novi[y[i]]);
    int klk=n;
    for(int i=0;i<n;i++) bio[i]=false;
    vector<pii> svi;
    for(int i=0;i<n;i++){
        if(bio[i]) continue;
        vector<int> sada;
        klk--;
        int tren=novi[i];
        sada.push_back(i);
        while(tren!=i){
            sada.push_back(tren);
            bio[tren]=true;
            tren=novi[tren];
        }
        swapovi(sada,svi);
    }
    ///
    vector<pii> res=popravi(n,naj,s,x,y,svi);
    for(int i=0;i<res.size();i++){
        p[i]=res[i].xx;
        q[i]=res[i].yy;
    }
}

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    int l=0;
    int r=m;
    int naj=-1;
    while(l<=r){
        int mid=l+(r-l)/2;
        if(moze(mid,n,m,s,x,y)){
            naj=mid;
            r=mid-1;
        }else l=mid+1;
    }
    nadji(naj,n,m,s,x,y,p,q);
	return naj;
}


Compilation message

sorting.cpp: In function 'bool moze(int, int, int, int*, int*, int*)':
sorting.cpp:16:29: warning: unused parameter 'm' [-Wunused-parameter]
   16 | bool moze(int mid,int n,int m,int* s,int* x,int* y){
      |                         ~~~~^
sorting.cpp: In function 'void swapovi(std::vector<int>, std::vector<std::pair<int, int> >&)':
sorting.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<niz.size()-1;i++){
      |                 ~^~~~~~~~~~~~~
sorting.cpp: In function 'std::vector<std::pair<int, int> > popravi(int, int, int*, int*, int*, std::vector<std::pair<int, int> >)':
sorting.cpp:51:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         if(i<svi.size()){
      |            ~^~~~~~~~~~~
sorting.cpp: In function 'void nadji(int, int, int, int*, int*, int*, int*, int*)':
sorting.cpp:84: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]
   84 |     for(int i=0;i<res.size();i++){
      |                 ~^~~~~~~~~~~
sorting.cpp:63:30: warning: unused parameter 'm' [-Wunused-parameter]
   63 | void nadji(int naj,int n,int m,int* s,int* x,int* y,int* p,int* q){
      |                          ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 300 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 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 204 KB Output is correct
8 Correct 1 ms 204 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 308 KB Output is correct
14 Correct 1 ms 300 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 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 440 KB Output is correct
25 Correct 1 ms 444 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 444 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 448 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 444 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 444 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 448 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 2 ms 444 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 176 ms 20220 KB Output is correct
16 Correct 158 ms 20560 KB Output is correct
17 Correct 181 ms 21560 KB Output is correct
18 Correct 73 ms 15048 KB Output is correct
19 Correct 145 ms 19032 KB Output is correct
20 Correct 162 ms 19412 KB Output is correct
21 Correct 149 ms 19604 KB Output is correct
22 Correct 151 ms 17012 KB Output is correct
23 Correct 177 ms 22264 KB Output is correct
24 Correct 169 ms 21944 KB Output is correct
25 Correct 187 ms 21692 KB Output is correct
26 Correct 153 ms 19460 KB Output is correct
27 Correct 149 ms 18848 KB Output is correct
28 Correct 191 ms 21840 KB Output is correct
29 Correct 175 ms 21456 KB Output is correct
30 Correct 110 ms 17684 KB Output is correct
31 Correct 180 ms 21940 KB Output is correct
32 Correct 179 ms 21536 KB Output is correct
33 Correct 183 ms 21976 KB Output is correct
34 Correct 174 ms 21916 KB Output is correct
35 Correct 147 ms 18760 KB Output is correct
36 Correct 67 ms 16108 KB Output is correct
37 Correct 200 ms 22568 KB Output is correct
38 Correct 181 ms 21768 KB Output is correct
39 Correct 180 ms 21868 KB Output is correct