답안 #432904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
432904 2021-06-18T15:02:54 Z balbit 정렬하기 (IOI15_sorting) C++14
0 / 100
2 ms 588 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define f first
#define s second

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#endif // BALBIT


#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int )((x).size())
#define pb push_back

#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)

#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
//signed main(){bug(1,2);}

const int maxn = 2e5+5;
bool seen[maxn];
int where[maxn];

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
//    int IT = 0;

    {
        int l = 0, r = M;
        while (l!=r) {
            int mid = (l+r)/2;
            vector<int> ts(n);
            REP(i,n) ts[i] = S[i];
            for (int j = 0; j<mid; ++j) {
                swap(ts[X[j]], ts[Y[j]]);
            }
            memset(seen, 0, sizeof seen);
                int tt = n;
            REP(i,n) {
                if (!seen[i]) {
                    --tt;
                    for (int j = i; ; j = S[j]) {
                        seen[j] = 1;
                        if (S[j] == i) break;
                    }
                }
            }
            if (tt <= mid) {
                r = mid;
            }else{
                l = mid+1;
            }
        }
        bug(l);
        vector<int> ts(n);
        REP(i,n) ts[i] = S[i];
        for (int j = 0; j<l; ++j) {
            swap(ts[X[j]], ts[Y[j]]);
        }
        int IT = 0;
        REP(i,n) where[S[i]] = i;
        for (int i = 0; i<n; ++i) {
            while (ts[i] != i) {
                {
                    swap(where[S[X[IT]]], where[S[Y[IT]]]);
                    swap(S[X[IT]], S[Y[IT]]);
                }
                int p1 = where[i], p2 = where[ts[i]];
                P[IT] = p1; Q[IT] = p2;
                ++IT;
                {
                    swap(where[S[p1]], where[S[p2]]);
                    swap(S[p1], S[p2]);
                }
                swap(ts[i], ts[ts[i]]);
                bug(i, ts[i]);
            }
        }
	return IT;
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Incorrect 1 ms 460 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Incorrect 1 ms 460 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Incorrect 1 ms 460 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Incorrect 1 ms 460 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 588 KB Output isn't correct
2 Halted 0 ms 0 KB -