1621C - Hidden Permutations - CodeForces Solution


dfs and similar interactive math *1700

Please click on ads to support us..

C++ Code:

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

#define V vector
#define L long
#define LL long long
#define P pair
#define VI V<int>
#define VLL V<LL>
#define VVI V<VI>
#define VVLL V<VLL>
#define VB V<bool>
#define PII P<int, int> 
#define PLL P<LL, LL>

#define loop(i,a,b) for(int i=a; i<b; i++)
#define pb push_back
#define FF first
#define SS second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define input(v,n)  loop(i,0,n)  cin>>v[i];
#define output(v,n) loop(i,0,n) cout<<v[i]<<" ";
#define nl cout<<"\n"
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define print(a) cout<<a<<"\n"
#define el cout<<endl;

const LL MOD = 1e9 + 7;
const double EPS = 1e-6;

/********************************************************************/

int query(int n) {
    cout << "? " << n << endl;
    int x; 
    cin >> x;
    return x;
}


void solve(){
    int n;
    cin >> n;

    vector<int> ans(n+1, -1);

    for(int i=1; i<=n; i++)
        if(ans[i] == -1) {
            int start = query(i);
            int prev = start;
            while(1) {
                int x = query(i);
                ans[prev] = x;
                prev = x;
                if(x == start)
                    break;
            }
        }

    cout << "! ";
    for(int i=1; i<=n; i++)
        cout << ans[i] << " ";
    cout << endl;
}

/********************************************************************/


int32_t main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif  

    int t;
    t = 1;
    cin>>t;
    while(t--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament