제출 #316643

#제출 시각아이디문제언어결과실행 시간메모리
316643jainbot27정렬하기 (IOI15_sorting)C++17
컴파일 에러
0 ms0 KiB
/*
Observation:
if we can do it in N turns, we can do it in N + 1

*/

#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()

#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;

int findSwapPairs(int n, int* s, int M, int* x, int *y, int *p, int *q){
    int l = 0, r = M + 1;
    vpii ans;
    while(l < r){
        int m = l + r; m = (m - (m&1)) / 2;
        vi pos(n);
        vi cur(n);
        vpii ops;
        F0R(i, n){
            cur[i] = s[i];
        }
        F0R(i, m){
            swap(cur[x[i]], cur[y[i]]);
        }
        F0R(i, n){
            pos[cur[i]] = i;
        }
        F0R(i, n){
            if(cur[i] != i){
                int x = cur[i];
                // whats currently at the spot we want
                int z = pos[i];
                // the pos of our current element
                ops.pb({i, pos[i]});
                cur[z] = x;
                cur[i] = i;
                pos[i] = i;
                pos[x] = z;
            }
        }
        if(siz(ops) <= m){
            ans = ops;
            r = m;
        }
        else{
            l = m + 1;
        }
    }
    F0R(i, l){
        if(i < ans.size()){
            p[i] = ans[i].f; q[i] = ans[i].s;
        }
        else{
            p[i] = 0; q[i] = 0;
        }
    }
    return l;
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int N;
    cin >> N;
    int S[N];
    F0R(i, N){
        cin >> S[i];
    }
    int M;
    cin >> M;
    int X[M], Y[M];
    F0R(i, M){
        cin >> X[i] >> Y[i];
    }
    int P[M], Q[M];
    int res = findSwapPairs(N, S, M, X, Y, P, Q);
    cout << res << nl; 
    F0R(i, res){
        cout << P[i] << ' ' << Q[i] << nl;
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:57:21: warning: declaration of 'int x' shadows a parameter [-Wshadow]
   57 |                 int x = cur[i];
      |                     ^
sorting.cpp:38:46: note: shadowed declaration is here
   38 | int findSwapPairs(int n, int* s, int M, int* x, int *y, int *p, int *q){
      |                                         ~~~~~^
sorting.cpp:77: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]
   77 |         if(i < ans.size()){
      |            ~~^~~~~~~~~~~~
/tmp/cc9nKuXd.o: In function `main':
grader.c:(.text.startup+0x0): multiple definition of `main'
/tmp/ccVXLKSW.o:sorting.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status