[Codeforces Round #669 (Div. 2)] C. Chocolate Bunny (交互,构造)

[Codeforces Round #669 (Div. 2)] C. Chocolate Bunny (交互,构造)

题面:

题意:

交互题,

系统有一个隐藏起来的(1dots n)的全排列,你可以做最多(2 imes n) 个询问,每一个询问如下:

你给出两个不同的数组下标(x,y,1 le x, y le n, x e y)

系统返回给你(p_x mod p_y)

你需要在最后输出那个隐藏的全排列。

思路:

我们分析取模运算可以得到以下性质:

我们如果做出两组询问,分别是:

(? x y)

(? y x)

得到的返回是(res_1,res_2)

很容易知道(也容易证明)如果(res_1>res_2),那么(p_x<p_y),否则(p_x>p_y)

并且(min(p_x,p_y)=max(res_1,res_2))。(这个性质非常重要。

那么我们就可以先假定排列中的最大值下标(mx)( ext 1),然后依次处理([2,n]),处理过程中维护(mx),然后利用上面的性质就可以把除了(p_i=n)的位置的数都填好,处理结束后的(mx)满足(p_{mx}=n)

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int ans[maxn];
int ask(int x, int y)
{
    cout << "? " << x << " " << y << endl;
    int res;
    cin >> res;
    return res;
}
int main()
{
#if DEBUG_Switch
    freopen("D:\code\input.txt", "r", stdin);
#endif
    //freopen("D:\code\output.txt","w",stdout);
    cin >> n;
    int mx = 1;
    repd(i, 2, n) {
        int a = ask(mx, i);
        int b = ask(i, mx);
        if (a > b) {
            ans[mx] = a;
            mx = i;
        } else {
            ans[i] = b;
        }
    }
    ans[mx] = n;
    cout << "! ";
    repd(i, 1, n) {
        cout << ans[i];
        if (i != n) {
            cout << " ";
        }
    }
    cout << endl;
    return 0;
}



本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
原文地址:https://www.cnblogs.com/qieqiemin/p/13639881.html