/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 2ms 532.0 KiB

Code

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int N;
        cin >> N;
        vector<int> v(N);
        for (int i = 0; i < N; ++i) {
            v[i] = i + 1;
        }

        // Special case: if N == 3, left rotation is better (2 3 1)
        // Otherwise swap adjacent pairs
        if (N % 2 == 0) {
            for (int i = 0; i < N; i += 2) {
                swap(v[i], v[i + 1]);
            }
        } else {
            // N is odd
            for (int i = 0; i + 3 < N; i += 2) {
                swap(v[i], v[i + 1]);
            }
            // Last 3 elements, apply left rotation
            int i = N - 3;
            int tmp = v[i];
            v[i] = v[i + 1];
            v[i + 1] = v[i + 2];
            v[i + 2] = tmp;
        }

        for (int x : v) cout << x << " ";
        cout << "\n";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1210 A. Smallest Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 18:48:17
Judged At
2025-07-14 18:48:17
Judged By
Score
100
Total Time
2ms
Peak Memory
532.0 KiB