답안 #331123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331123 2020-11-27T12:42:09 Z Vladth11 정렬하기 (IOI15_sorting) C++14
100 / 100
274 ms 24908 KB
#include <bits/stdc++.h>
#include "sorting.h"
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> piii;

const ll NMAX = 200001;
const ll INF = (1 << 30);
const ll MOD = 1000000007;
const ll BLOCK = 101;
const ll nr_of_bits = 20;
const ll delta = 0.0000001;

int viz[NMAX];
int cop[NMAX];

bool OK(int p, int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    for(int i = 0; i < N; i++)
        S[i] = cop[i];
    for(int i = 0; i < p; i++) {
        swap(S[X[i]], S[Y[i]]);
    }
    for(int i = 0; i < N; i++)
        viz[i] = 0;
    int cc = 0;
    for(int i = 0; i < N; i++) {
        vector <int> ciclu;
        if(viz[i])
            continue;
        int pz = i;
        while(!viz[pz]) {
            ciclu.push_back(pz);
            viz[pz] = 1;
            pz = S[pz];
        }
        cc += ciclu.size() - 1;
    }
    return (cc <= p);
}

pii pr[NMAX * 3];
int poz[NMAX];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    P[0] = 0;
    Q[0] = 0;
    int i;
    int r = 0, pas = 1 << 20;
    for(int i = 0; i < N; i++) {
        cop[i] = S[i];
    }
    while(pas) {
        if(r + pas <= M && !OK(r + pas, N, S, M, X, Y, P, Q))
            r += pas;
        pas /= 2;
    }
    if(!OK(r + pas, N, S, M, X, Y, P, Q))
        r++;
    for(int i = 0; i < N; i++)
        S[i] = cop[i];
    for(int i = 0; i < r; i++) {
        swap(S[X[i]], S[Y[i]]);
    }
    for(int i = 0; i < N; i++) {
        viz[i] = 0;
    }
    int cc = 0;
    for(int i = 0; i < N; i++) {
        vector <int> ciclu;
        if(viz[i])
            continue;
        int pz = i;
        while(!viz[pz]) {
            ciclu.push_back(pz);
            viz[pz] = 1;
            pz = S[pz];
        }
        for(int j = ciclu.size() - 2; j >= 0; j--) {
            pr[++cc] = {S[ciclu[j + 1]], S[ciclu[j]]};
            swap(ciclu[j], ciclu[j + 1]);
        }
    }
    for(int i = N - 1; i >= 0; i--) {
        S[i] = cop[i];
    }
    for(int i = 0; i < N; i++) {
        poz[S[i]] = i;
    }
    for(int i = 0; i < r; i++) {
        int a = S[X[i]], b = S[Y[i]];
        swap(poz[a], poz[b]);
        swap(S[X[i]], S[Y[i]]);
        P[i] = poz[pr[i + 1].first];
        Q[i] = poz[pr[i + 1].second];
        a = P[i];
        b = Q[i];
        swap(poz[pr[i + 1].first], poz[pr[i + 1].second]);
        swap(S[a], S[b]);
    }
    return r;
}

Compilation message

sorting.cpp:16:18: warning: conversion from 'double' to 'll' {aka 'long long int'} changes value from '9.9999999999999995e-8' to '0' [-Wfloat-conversion]
   16 | const ll delta = 0.0000001;
      |                  ^~~~~~~~~
sorting.cpp: In function 'bool OK(int, int, int*, int, int*, int*, int*, int*)':
sorting.cpp:40:30: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   40 |         cc += ciclu.size() - 1;
      |                              ^
sorting.cpp:21:36: warning: unused parameter 'M' [-Wunused-parameter]
   21 | bool OK(int p, int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                ~~~~^
sorting.cpp:21:61: warning: unused parameter 'P' [-Wunused-parameter]
   21 | bool OK(int p, int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                         ~~~~^~~
sorting.cpp:21:70: warning: unused parameter 'Q' [-Wunused-parameter]
   21 | bool OK(int p, int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                                                  ~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:53:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   53 |     for(int i = 0; i < N; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:63:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   63 |     for(int i = 0; i < N; i++)
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:65:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   65 |     for(int i = 0; i < r; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:68:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   68 |     for(int i = 0; i < N; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:72:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   72 |     for(int i = 0; i < N; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:82:34: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   82 |         for(int j = ciclu.size() - 2; j >= 0; j--) {
      |                     ~~~~~~~~~~~~~^~~
sorting.cpp:87:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   87 |     for(int i = N - 1; i >= 0; i--) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:90:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   90 |     for(int i = 0; i < N; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:93:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   93 |     for(int i = 0; i < r; i++) {
      |             ^
sorting.cpp:51:9: note: shadowed declaration is here
   51 |     int i;
      |         ^
sorting.cpp:51:9: warning: unused variable 'i' [-Wunused-variable]
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 2 ms 620 KB Output is correct
24 Correct 2 ms 620 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 1 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 620 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 2 ms 620 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 620 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 2 ms 620 KB Output is correct
11 Correct 2 ms 620 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 193 ms 22240 KB Output is correct
16 Correct 214 ms 22192 KB Output is correct
17 Correct 244 ms 24036 KB Output is correct
18 Correct 171 ms 16124 KB Output is correct
19 Correct 260 ms 21076 KB Output is correct
20 Correct 269 ms 21540 KB Output is correct
21 Correct 266 ms 21244 KB Output is correct
22 Correct 187 ms 18872 KB Output is correct
23 Correct 228 ms 24908 KB Output is correct
24 Correct 228 ms 24756 KB Output is correct
25 Correct 217 ms 24248 KB Output is correct
26 Correct 256 ms 20716 KB Output is correct
27 Correct 235 ms 19820 KB Output is correct
28 Correct 255 ms 23280 KB Output is correct
29 Correct 274 ms 22380 KB Output is correct
30 Correct 252 ms 18668 KB Output is correct
31 Correct 272 ms 22892 KB Output is correct
32 Correct 216 ms 23484 KB Output is correct
33 Correct 225 ms 24576 KB Output is correct
34 Correct 216 ms 23660 KB Output is correct
35 Correct 258 ms 20212 KB Output is correct
36 Correct 168 ms 16748 KB Output is correct
37 Correct 250 ms 24572 KB Output is correct
38 Correct 240 ms 23372 KB Output is correct
39 Correct 242 ms 23548 KB Output is correct