답안 #687059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
687059 2023-01-26T05:41:42 Z Alihan_8 Cat (info1cup19_cat) C++17
100 / 100
569 ms 29604 KB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
void solve(){
    int n; cin >> n;
    vector <int> p(n), pos(n);
    for ( auto &i: p ) cin >> i, i--;
    vector <pair<int,int>> res;
    auto inv = [&](int i){
        return n-1-i;
    };
    auto answer = [&](int i, int j){
        int _i = inv(i), _j = inv(j);
        swap(p[i], p[j]), swap(pos[p[i]], pos[p[j]]);
        swap(p[_i], p[_j]), swap(pos[p[_i]], pos[p[_j]]);
        res.pb({i, j});
    };
    auto print = [&](){
        cout << (int)res.size() << ' ' << (int)res.size() << ln;
        for ( auto [l, r]: res ) cout << ++l << ' ' << ++r << ln;
    };
    for ( int i = 0; i < n; i++ ) pos[p[i]] = i;
    vector <int> rem;
    for ( int i = 0; i < n/2; i++ ){
        if ( pos[i] == i ) continue;
        if ( inv(pos[i]) < i ){
            cout << "-1\n";
            return;
        }
        if ( inv(i) == pos[i] ){
            rem.pb(i);
            continue;
        }
        answer(i, pos[i]);
    }
    if ( (int)rem.size() & 1 ){
        cout << "-1\n";
        return;
    }
    for ( int i = 1; i < (int)rem.size(); i += 2 ){
        answer(rem[i], inv(rem[i-1]));
        answer(rem[i], rem[i-1]);
    }
    for ( int i = 0; i < n; i++ ){
        if ( pos[i] != i ){
            cout << "-1\n";
            return;
        }
    }
    print();
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

    int T; cin >> T;
    while ( T-- ){
        solve();
    }

 	cout << '\n';
}

Compilation message

cat.cpp: In function 'void IO(std::string)':
cat.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cat.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 596 KB Output is correct
2 Correct 20 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 18 ms 596 KB Output is correct
3 Correct 20 ms 980 KB Output is correct
4 Correct 22 ms 1144 KB Output is correct
5 Correct 8 ms 596 KB Output is correct
6 Correct 7 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 596 KB Output is correct
2 Correct 20 ms 980 KB Output is correct
3 Correct 569 ms 28328 KB Output is correct
4 Correct 449 ms 25468 KB Output is correct
5 Correct 449 ms 29516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 18 ms 596 KB Output is correct
3 Correct 20 ms 980 KB Output is correct
4 Correct 22 ms 1144 KB Output is correct
5 Correct 8 ms 596 KB Output is correct
6 Correct 7 ms 468 KB Output is correct
7 Correct 569 ms 28328 KB Output is correct
8 Correct 449 ms 25468 KB Output is correct
9 Correct 449 ms 29516 KB Output is correct
10 Correct 504 ms 24632 KB Output is correct
11 Correct 383 ms 24040 KB Output is correct
12 Correct 491 ms 28700 KB Output is correct
13 Correct 487 ms 29604 KB Output is correct
14 Correct 416 ms 26916 KB Output is correct