답안 #795555

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
795555 2023-07-27T11:15:27 Z Markynoodle 정렬하기 (IOI15_sorting) C++17
0 / 100
11 ms 436 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

#define all(x) x.begin(),x.end()
#define ll long long
#define fastOI ios_base::sync_with_stdio(false); \
cin.tie(nullptr);
auto cmp = [](int a, int b){return a > b;};

vector<int> test;
vector<int> visited;
int n;
void dfs(int u){
    visited[u] = 1;
    if(visited[test[u]] == 1)return;
    dfs(test[u]);
    return;
}
vector<pair<int,int>> pairstodo;

void dfspair(int u, int v){
    visited[u] = 1;
    if(u != v){
        pairstodo.push_back({u, v});
    }
    if(visited[test[u]] == 1)return;
    dfspair(test[u], u);
    return;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int mini = 0;
    n = N;
    vector<int> start;
    for(int i =0; i<N; i++){
        start.push_back(S[i]);
    }
    test = start;
    for(int i =0; i<=N; i++){
        if(i != 0)swap(test[X[i-1]], test[Y[i-1]]);
        //for(auto k : test)cout<<k<<" ";
        visited.assign(n, 0);
        int counter = n;
        for(int j =0; j<n; j++){
            if(visited[j] == 1)continue;
            counter--;
            dfs(j);
        }
        //cout<<counter<<"\n";
        if(counter <= i){
            mini = i;
            break;
        }
    }
    //cout<<mini<<" ";
    //for(auto k : test)cout<<k<<" ";
    //cout<<"\n";
    visited.assign(n, 0);
    for(int i =0; i<n; i++){
        if(visited[i] == 1)continue;
        dfspair(i, i);
    }
    //for(auto k : pairstodo)cout<<k.first<<" "<<k.second<<"\n";
    vector<int> posholder(n);
    for(int i =0; i<n; i++){
        posholder[start[i]] = i;
    }
    test = start;
    for(int i =0; i<mini; i++){
        posholder[test[X[i]]] = test[Y[i]];
        posholder[test[Y[i]]] = test[X[i]];
        swap(test[X[i]], test[Y[i]]);
        P[i] = posholder[pairstodo[i].first];
        Q[i] = posholder[pairstodo[i].second];

    }
    //cout<<mini<<"\n";
    //for(int i =0; i<mini; i++)cout<<P[i]<<" "<<Q[i]<<"\n";

    return mini;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:32:39: warning: unused parameter 'M' [-Wunused-parameter]
   32 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 436 KB Output isn't correct
2 Halted 0 ms 0 KB -