/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 3ms 532.0 KiB
#2 Wrong Answer 2ms 324.0 KiB

Code

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; 
    cin >> T;
    while (T--){
        int N;
        cin >> N;

        // Small special‐cases
        if (N == 1) {
            cout << "1\n";
            continue;
        }
        if (N <= 4) {
            cout << "-1\n";
            continue;
        }

        vector<int> ans;
        ans.reserve(N);
        vector<bool> used(N+1,false);

        // We'll start with an even; you can also try starting with an odd.
        int parity = 0;  // 0 = even, 1 = odd
        bool ok = true;

        for (int pos = 0; pos < N; pos++, parity ^= 1) {
            bool placed = false;
            // scan numbers of the required parity
            for (int x = (parity==0 ? 2 : 1); x <= N; x += 2) {
                if (used[x]) continue;
                if (pos > 0) {
                    int prev = ans[pos-1];
                    // abs difference must exceed 1
                    if (abs(prev - x) <= 1) 
                        continue;
                }
                // place x
                used[x] = true;
                ans.push_back(x);
                placed = true;
                break;
            }
            if (!placed) {
                ok = false;
                break;
            }
        }

        if (!ok || (int)ans.size() != N) {
            cout << "-1\n";
        } else {
            for (int i = 0; i < N; i++) {
                if (i) cout << ' ';
                cout << ans[i];
            }
            cout << "\n";
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1203 D. Roy Loves Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-08 14:12:29
Judged At
2025-06-08 14:12:29
Judged By
Score
0
Total Time
3ms
Peak Memory
532.0 KiB