답안 #312250

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
312250 2020-10-12T21:35:10 Z aZvezda 정렬하기 (IOI15_sorting) C++14
100 / 100
196 ms 31188 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 2e5 + 10;
int n, m;
vector<pair<int, int> > swps;
vector<int> start;
bool used[MAX_N];
int inv[MAX_N];

int countCycles(vector<int> &a) {
    int cnt = 0;
    for(int i = 0; i < a.size(); i ++) {used[i] = false;}
    for(int i = 0; i < a.size(); i ++) {
        if(used[i]) {continue;}
        cnt ++;
        int curr = i;
        while(!used[curr]) {
            used[curr] = true;
            curr = a[curr];
        }
    }
    return cnt;
}

vector<pair<int, int> > getPairs(vector<int> &a) {
    vector<pair<int, int> > ret;
    for(int i = 0; i < a.size(); i ++) {used[i] = false;}
    for(int i = 0; i < a.size(); i ++) {
        if(used[i]) {continue;}
        int curr = i, ind = i;
        while(!used[curr]) {
            used[curr] = true;
            curr = a[curr];
            if(curr != i) {
                ret.push_back({curr, ind});
            }
            ind = curr;
        }
    }
    return ret;
}

bool eval(int x) {
    vector<int> toAsk = start;
    for(int i = 0; i < x; i ++) {
        swap(toAsk[swps[i].first], toAsk[swps[i].second]);
    }
    //cout << "Eval " << x << " " << countCycles(toAsk) << endl;
    return n - countCycles(toAsk) <= x;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    m = M;
    for(int i = 0; i < n; i ++) {
        start.push_back(S[i]);
    }
    for(int i = 0; i < m; i ++) {
        swps.push_back({X[i], Y[i]});
    }
    if(countCycles(start) == n) {
        //cout << "Here " << endl;
        return 0;
    }
    int l = 0, r = m;
    while(l < r - 1) {
        int mid = (l + r) / 2ll;
        if(eval(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    vector<int> atEnd = start;
    for(int i = 0; i < r; i ++) {
        swap(atEnd[swps[i].first], atEnd[swps[i].second]);
    }
    for(int i = 0; i < n; i ++) {
        inv[atEnd[i]] = i;
    }
    auto toRet = getPairs(atEnd);
    vector<pair<int, int> > ret;
    for(int i = 0; i < r; i ++) {
        int ind = r - 1 - i;
        if(i >= toRet.size()) {
            ret.push_back({0, 0});
        } else {
            //cout << "Need " << toRet[i].first << " " << toRet[i].second << endl;
            ret.push_back({inv[toRet[i].first], inv[toRet[i].second]});
        }
        swap(inv[atEnd[swps[ind].first]], inv[atEnd[swps[ind].second]]);
        swap(atEnd[swps[ind].first], atEnd[swps[ind].second]);
    }
    reverse(ret.begin(), ret.end());
    for(int i = 0; i < r; i ++) {
        P[i] = ret[i].first;
        Q[i] = ret[i].second;
    }
	return r;
}


Compilation message

sorting.cpp: In function 'int countCycles(std::vector<int>&)':
sorting.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for(int i = 0; i < a.size(); i ++) {used[i] = false;}
      |                    ~~^~~~~~~~~~
sorting.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < a.size(); i ++) {
      |                    ~~^~~~~~~~~~
sorting.cpp: In function 'std::vector<std::pair<int, int> > getPairs(std::vector<int>&)':
sorting.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 0; i < a.size(); i ++) {used[i] = false;}
      |                    ~~^~~~~~~~~~
sorting.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for(int i = 0; i < a.size(); i ++) {
      |                    ~~^~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:96:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         if(i >= toRet.size()) {
      |            ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 652 KB Output is correct
22 Correct 2 ms 768 KB Output is correct
23 Correct 2 ms 768 KB Output is correct
24 Correct 2 ms 768 KB Output is correct
25 Correct 2 ms 768 KB Output is correct
26 Correct 2 ms 768 KB Output is correct
27 Correct 2 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 636 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 768 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 636 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 768 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 636 KB Output is correct
15 Correct 173 ms 27824 KB Output is correct
16 Correct 177 ms 28372 KB Output is correct
17 Correct 195 ms 29784 KB Output is correct
18 Correct 57 ms 22500 KB Output is correct
19 Correct 155 ms 25636 KB Output is correct
20 Correct 164 ms 26636 KB Output is correct
21 Correct 163 ms 26708 KB Output is correct
22 Correct 162 ms 25236 KB Output is correct
23 Correct 185 ms 30836 KB Output is correct
24 Correct 196 ms 30420 KB Output is correct
25 Correct 192 ms 30068 KB Output is correct
26 Correct 173 ms 26580 KB Output is correct
27 Correct 147 ms 25556 KB Output is correct
28 Correct 195 ms 30040 KB Output is correct
29 Correct 183 ms 29280 KB Output is correct
30 Correct 115 ms 23936 KB Output is correct
31 Correct 194 ms 29908 KB Output is correct
32 Correct 182 ms 29776 KB Output is correct
33 Correct 193 ms 30432 KB Output is correct
34 Correct 191 ms 30296 KB Output is correct
35 Correct 153 ms 25428 KB Output is correct
36 Correct 68 ms 23524 KB Output is correct
37 Correct 195 ms 31188 KB Output is correct
38 Correct 187 ms 30160 KB Output is correct
39 Correct 192 ms 30288 KB Output is correct