답안 #1065847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065847 2024-08-19T12:18:33 Z anango 정렬하기 (IOI15_sorting) C++17
100 / 100
360 ms 35836 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[]) {
    int l = 0;
    int r = M;
    while (l<r) {
        int m = (l+r)/2;
        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; 
        if (swaps.size()>m) {
            l=m+1;
            r=r;
        }
        else {
            l=l;
            r=m;
        }
        
    }
    M = l;
	 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++) {
        }
        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:43:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |             for (int j=1; j<i.size(); j++) {
      |                           ~^~~~~~~~~
sorting.cpp:49:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   49 |         if (swaps.size()>m) {
      |             ~~~~~~~~~~~~^~
sorting.cpp:73:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         for (int j=1; j<i.size(); j++) {
      |                       ~^~~~~~~~~
sorting.cpp:80: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]
   80 |     while (swaps.size()<M) {
      |            ~~~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 432 KB Output is correct
5 Correct 0 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 344 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 432 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 432 KB Output is correct
5 Correct 0 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 0 ms 348 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 696 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 856 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 520 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 856 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 520 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 3 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 234 ms 23656 KB Output is correct
16 Correct 253 ms 23852 KB Output is correct
17 Correct 247 ms 25768 KB Output is correct
18 Correct 303 ms 35836 KB Output is correct
19 Correct 318 ms 30584 KB Output is correct
20 Correct 310 ms 32152 KB Output is correct
21 Correct 360 ms 32740 KB Output is correct
22 Correct 239 ms 21032 KB Output is correct
23 Correct 257 ms 26844 KB Output is correct
24 Correct 260 ms 26180 KB Output is correct
25 Correct 250 ms 26256 KB Output is correct
26 Correct 316 ms 33836 KB Output is correct
27 Correct 303 ms 33572 KB Output is correct
28 Correct 273 ms 26592 KB Output is correct
29 Correct 264 ms 27212 KB Output is correct
30 Correct 333 ms 34184 KB Output is correct
31 Correct 253 ms 27748 KB Output is correct
32 Correct 242 ms 25824 KB Output is correct
33 Correct 262 ms 26332 KB Output is correct
34 Correct 263 ms 25712 KB Output is correct
35 Correct 340 ms 30692 KB Output is correct
36 Correct 335 ms 34364 KB Output is correct
37 Correct 258 ms 29252 KB Output is correct
38 Correct 234 ms 27004 KB Output is correct
39 Correct 284 ms 27180 KB Output is correct