답안 #1065820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065820 2024-08-19T12:01:55 Z anango 정렬하기 (IOI15_sorting) C++17
54 / 100
10 ms 1040 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int n;


vector<vector<int>> getcycledecomposition(vector<int> p) {
    vector<int> visited(n,0);
    vector<vector<int>> cycles;
    for (int i=0; i<n; i++) {
        if (!visited[i]) {
            visited[i] = 1;
            vector<int> cycle={i};
            while (p[cycle.back()]!=i) {
                visited[p[cycle.back()]] = 1;
                cycle.push_back(p[cycle.back()]);
            }
            cycles.push_back(cycle);
        }
    }
    return cycles;
}


int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n=N;
    vector<int> rev(n); for (int i=0; i<n; i++) rev[i] = i;
    vector<int> revpos(n); for (int i=0; i<n; i++) revpos[i] = i;
    vector<int> pos(n); for (int i=0; i<n; i++) pos[i] = i;
    vector<int> seq(n); for (int i=0; i<n; i++) seq[i] = S[i];
    for (int i=0; i<M; i++) {
        swap(pos[X[i]],pos[Y[i]]);
    }
    vector<int> reqperm(n,-1); for (int i=0; i<n; i++) reqperm[i] = seq[pos[i]];
    vector<int> reverse_finalpos(n,-1); for (int i=0; i<n; i++) reverse_finalpos[pos[i]] = i;
    vector<vector<int>> cycles = getcycledecomposition(reqperm);
    vector<pair<int,int>> swaps;
    for (auto i:cycles) {
        for (int j=1; j<i.size(); j++) {
            swaps.push_back({i[0],i[j]});
        }
    }
    reverse(swaps.begin(), swaps.end());
    vector<int> state(n); for (int i=0; i<n; i++) state[i] = i; //the current 'pos'
    //swaps={{0,3},{0,2}};
    while (swaps.size()<M) {
        swaps.push_back({0,0});
    }
    
    vector<int> revperm(n); for (int i=0; i<n; i++) revperm[S[i]] = i;
    vector<int> perm(n); for (int i=0; i<n; i++) perm[i] = S[i];
    int bloaters = 0;
    for (int i=0; i<n; i++) {
        if (perm[i]!=i) bloaters++;
    }
    int lastswap = -1;
    for (int sw=0; sw<M; sw++) {
        swap(revperm[perm[X[sw]]],revperm[perm[Y[sw]]]);
        bloaters-=perm[X[sw]]!=X[sw];
        bloaters-=perm[Y[sw]]!=Y[sw];
        swap(perm[X[sw]],perm[Y[sw]]);
        bloaters+=perm[X[sw]]!=X[sw];
        bloaters+=perm[Y[sw]]!=Y[sw];
        pair<int,int> swp = swaps[sw];
        int a = swp.first;
        int b = swp.second;
        //cout << "trying2 " << a <<" " << b << endl;
        a=revperm[a]; b=revperm[b];
        //cout << "trying3 " << a <<" " << b << endl;
        swap(revperm[perm[a]],revperm[perm[b]]);
        bloaters-=perm[a]!=a;
        bloaters-=perm[b]!=b;
        swap(perm[a],perm[b]);
        bloaters+=perm[a]!=a;
        bloaters+=perm[b]!=b;
        P[sw] = a; Q[sw] = b;
        //cout << "swapping " << a <<" " << b<<endl;
        //cout << "ITERATION " << sw << endl;
        for (int i=0; i<n; i++) {
            assert(revperm[perm[i]]==i);
        }
        if (bloaters==0) {
            lastswap=sw;
            break;
        }
        /*for (int i=0; i<n; i++) {
            cout << revperm[i] <<" ";
        }
        cout << endl;
        for (int i=0; i<n; i++) {
            cout << perm[i] <<" ";
        }
        cout << endl;*/
    }
    
    

    /*for (int i=0; i<n; i++) {
        cout << pos[i] << " ";
    }
    cout << endl;
    for (int i=0; i<swaps.size(); i++) {
        cout << swaps[i].first <<" " << swaps[i].second << endl;
    }
    cout << endl;
    for (int i=0; i<n; i++) {
        cout << reqperm[i] << " ";
    }
    cout << endl;*/
    vector<int> cur(n); for (int i=0; i<n; i++) cur[i] = S[i];
    /*for (int i=0; i<M; i++) {
        swap(cur[X[i]],cur[Y[i]]);
        swap(cur[P[i]],cur[Q[i]]);
    }
    for (int i=0; i<n; i++) {
        assert(cur[i]==i);
        if (cur[i]!=i) {
            while (1) {
                int o=0;
            }
        }
    }*/
    /*cout << "final result" << endl;
    for (int i=0; i<n; i++) {
        cout << cur[i] <<" ";
    }
    cout << endl;*/

	return lastswap+1;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:39:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for (int j=1; j<i.size(); j++) {
      |                       ~^~~~~~~~~
sorting.cpp:46:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |     while (swaps.size()<M) {
      |            ~~~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 388 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 444 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 388 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 5 ms 1004 KB Output is correct
22 Correct 5 ms 1000 KB Output is correct
23 Correct 6 ms 1000 KB Output is correct
24 Correct 5 ms 1040 KB Output is correct
25 Correct 10 ms 996 KB Output is correct
26 Correct 5 ms 924 KB Output is correct
27 Correct 6 ms 964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -