結果

問題 No.2732 Similar Permutations
ユーザー GOTKAKO
提出日時 2024-04-19 23:45:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,461 bytes
コンパイル時間 2,927 ms
コンパイル使用メモリ 214,300 KB
最終ジャッジ日時 2025-02-21 05:47:47
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 87 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

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

random_device rnd;
mt19937 mt(rnd());

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    double st = clock();
    
    int N; cin >> N;
    if(N == 1){cout << -1 << endl; return 0;}
    vector<pair<int,int>> A(N);
    for(int i=0; i<N; i++) cin >> A.at(i).first,A.at(i).second = i;


    if(N < 10){
        vector<vector<int>> M(1e6);
        vector<int> P(N);
        iota(P.begin(),P.end(),1);
        do{
            int Xor = 0;
            for(int i=0; i<N; i++) Xor ^= A.at(i).first+P.at(A.at(i).second);
            if(M.at(Xor).size()){
                for(int i=0; i<N; i++) cout << P.at(i) << " "; cout << endl;
                for(int i=0; i<N; i++) cout << M.at(Xor).at(i) << " "; cout << endl;
                return 0;
            }
            M.at(Xor) = P;
        }while(next_permutation(P.begin(),P.end()));
		cout << -1 << endl; return 0;
    }
    sort(A.begin(),A.end());
    set<int> idx;
    for(int i=1; i<=N; i++) idx.insert(i);

    int swap1 = -1,swap2 = -1;
    vector<int> answer(N);
    for(int i=0; i<N-1; i++){
        auto[a1,pos1] = A.at(i); auto[a2,pos2] = A.at(i+1);
        if(a1 == a2){
            swap1 = pos1,swap2 = pos2;
            answer.at(pos1) = 1,answer.at(pos2) = 2;
            idx.erase(1); idx.erase(2); break;
        }
    }
    if(swap1 == -1){
        while(swap1 == -1){
            double no = clock();
            if((no-st)/CLOCKS_PER_SEC >= 1.8) break;
            int pos1 = mt()%N,pos2 = pos1;
            while(pos2 == pos1){pos2 = mt()%N;}

            auto[a1,p1] = A.at(pos1); auto[a2,p2] = A.at(pos2);
            for(int d=0; d<20; d++) for(int d2=d+1; d2<20; d2++){
                if(N > (1<<d2) || a1&(1<<d) || a2&(1<<d2) || a1&(1<<d2) || a2&(1<<d) || swap1 != -1) continue;
                swap1 = p1,swap2 = p2;
                answer.at(p1) = 1<<d; answer.at(p2) = 1<<d2;
                idx.erase(1<<d); idx.erase(1<<d2); 
            }
        }
    }
    if(swap1 == -1){cout << -1 << endl; return 0;}

    for(auto &a : answer) if(a == 0) a = *idx.begin(),idx.erase(idx.begin());
    for(int i=0; i<N; i++) cout << answer.at(i) << (i==N-1?"\n":" ");
    for(int i=0; i<N; i++){
        if(i != swap1 && i != swap2) cout << answer.at(i) << (i==N-1?"\n":" ");
        if(i == swap1) cout << answer.at(swap2) << (i==N-1?"\n":" ");
        if(i == swap2) cout << answer.at(swap1) << (i==N-1?"\n":" ");
    }
}
0