結果

問題 No.2732 Similar Permutations
ユーザー GOTKAKO
提出日時 2024-04-19 23:21:53
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,878 bytes
コンパイル時間 9,020 ms
コンパイル使用メモリ 271,120 KB
最終ジャッジ日時 2025-02-21 05:37:38
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 TLE * 1
other AC * 92 WA * 9
権限があれば一括ダウンロードができます

ソースコード

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;
    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<<d) || a1&(1<<d2) || a2&(1<<d2) || 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