/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 384.0 KiB
#2 Time Exceeded ≥1592ms ≥604.0 KiB
#3 Time Exceeded ≥1598ms ≥532.0 KiB

Code

// #include<bits/stdc++.h>
// using namespace std;
// #define Faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// #define endl '\n'
// #define ll long long
// #define test_case int t;cin>>t;while(t--)


// int main(){


//         Faster;


       
// }

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

#define endl '\n'

bool isValid(int a, int b) {
    return __gcd(a, b) == 1 && abs(a - b) > 1;
}

bool dfs(vector<int>& arr, vector<bool>& used, int n) {
    if ((int)arr.size() == n) return true;

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            if (arr.empty() || isValid(arr.back(), i)) {
                arr.push_back(i);
                used[i] = true;

                if (dfs(arr, used, n)) return true;

                arr.pop_back();
                used[i] = false;
            }
        }
    }
    return false;
}

void solve(int n) {
    vector<int> arr;
    vector<bool> used(n + 1, false);

    if (dfs(arr, used, n)) {
        for (int x : arr) cout << x << " ";
        cout << endl;
    } else {
        cout << -1 << endl;
    }
}

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

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






Information

Submit By
Type
Submission
Problem
P1203 D. Roy Loves Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-30 12:37:39
Judged At
2025-07-30 12:37:39
Judged By
Score
2
Total Time
≥1598ms
Peak Memory
≥604.0 KiB