답안 #31823

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
31823 2017-09-10T13:45:32 Z top34051 정렬하기 (IOI15_sorting) C++14
100 / 100
223 ms 13916 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005

int n, s[maxn];
int a[maxn], pos[maxn];
pair<int,int> sw[maxn];

int get(int rec) {
    int i,x,cnt;
    for(i=0;i<n;i++) a[i] = s[i];
    for(i=0;i<n;i++) pos[a[i]] = i;
    cnt = 0;
    for(i=0;i<n;i++) {
        x = pos[i];
        if(i!=x) {
            pos[a[i]] = x;
            swap(a[i], a[x]);
            if(rec) sw[cnt] = {a[i],a[x]};
            cnt++;
        }
    }
    return cnt;
}

bool check(int x,int S[],int X[],int Y[]) {
    int i;
    for(i=0;i<n;i++) s[i] = S[i];
    for(i=0;i<=x;i++) swap(s[X[i]], s[Y[i]]);
    if(get(0)<=x+1) return 1;
    else return 0;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int i,x,now,l,r,mid;
    n = N;
    for(i=0;i<n;i++) s[i] = S[i];
    l = -1; r = N;
    while(l<=r) {
        mid = (l+r)/2;
        if(check(mid,S,X,Y)) now = mid, r = mid-1;
        else l = mid+1;
    }
    check(now,S,X,Y);
    get(1);
    for(i=0;i<n;i++) s[i] = S[i];
    for(i=0;i<n;i++) pos[s[i]] = i;
    for(i=0;i<now+1;i++) {
        swap(pos[s[X[i]]], pos[s[Y[i]]]);
        swap(s[X[i]], s[Y[i]]);
        P[i] = pos[sw[i].first];
        Q[i] = pos[sw[i].second];
        swap(pos[s[P[i]]], pos[s[Q[i]]]);
        swap(s[P[i]], s[Q[i]]);
    }
    return now+1;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:36:11: warning: unused variable 'x' [-Wunused-variable]
     int i,x,now,l,r,mid;
           ^
sorting.cpp:35:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
sorting.cpp:36:13: warning: 'now' may be used uninitialized in this function [-Wmaybe-uninitialized]
     int i,x,now,l,r,mid;
             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 168 ms 12328 KB Output is correct
16 Correct 175 ms 12712 KB Output is correct
17 Correct 213 ms 13304 KB Output is correct
18 Correct 61 ms 7672 KB Output is correct
19 Correct 168 ms 10564 KB Output is correct
20 Correct 164 ms 11128 KB Output is correct
21 Correct 183 ms 11128 KB Output is correct
22 Correct 162 ms 13408 KB Output is correct
23 Correct 181 ms 13688 KB Output is correct
24 Correct 220 ms 13560 KB Output is correct
25 Correct 214 ms 13380 KB Output is correct
26 Correct 160 ms 11096 KB Output is correct
27 Correct 142 ms 10420 KB Output is correct
28 Correct 201 ms 13308 KB Output is correct
29 Correct 212 ms 12664 KB Output is correct
30 Correct 114 ms 9480 KB Output is correct
31 Correct 200 ms 12920 KB Output is correct
32 Correct 179 ms 13308 KB Output is correct
33 Correct 210 ms 13560 KB Output is correct
34 Correct 195 ms 13636 KB Output is correct
35 Correct 139 ms 10616 KB Output is correct
36 Correct 51 ms 8056 KB Output is correct
37 Correct 223 ms 13916 KB Output is correct
38 Correct 182 ms 13432 KB Output is correct
39 Correct 213 ms 13432 KB Output is correct