답안 #1065799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065799 2024-08-19T11:51:10 Z anango 정렬하기 (IOI15_sorting) C++17
0 / 100
14 ms 712 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];
    for (int sw=0; sw<M; sw++) {
        swap(revperm[perm[X[sw]]],revperm[perm[Y[sw]]]);
        swap(perm[X[sw]],perm[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]]);
        swap(perm[a],perm[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);
        }
        /*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]]);
    }
    cout << "final result" << endl;
    for (int i=0; i<n; i++) {
        cout << cur[i] <<" ";
    }
    cout << endl;
	return M;
}


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 Incorrect 0 ms 344 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 712 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 712 KB Expected integer, but "ITERATION" found
2 Halted 0 ms 0 KB -