1. 程式人生 > 實用技巧 >Codeforces-1470C(Chocolate Bunny)

Codeforces-1470C(Chocolate Bunny)

Chocolate Bunny

題目大意:互動題,題意不難理解

解題思路:解這道題首先要知道一個結論 如果 px%py >py%px 則 px < py具體證明可以看官方題解

接下來就是求解每一個數, 我們可以先假設最大的數再位置1,那麼我們從第二個位置開始詢問,若這個位置的數要比最大的數大,則那麼我們假設最大數的位置就要進行更新,這個值就是px%py然後把最大值所在的位置移動到第二個位置就行,如比他小的話,就可以從py%px的到第二個位置的值則沒進行兩次詢問就可以得到一個位置的值,則總共進行2*n-2次就可以的帶除最大位置外的值

官方題解 :https://codeforces.com/blog/entry/82417

題目連結:https://vjudge.net/problem/CodeForces-1407C

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e4+10;
int ans[maxn];
inline ll read(){
    ll s=0,w=1;char ch = getchar();
    while(ch<48 || ch>57) {
        if(ch=='-') w=-1;ch = getchar();
    }
    while
(ch>=48&&ch<=57) s = (s<<1) + (s<<3) + (ch^48),ch=getchar(); return s*w; } int pos(int x,int y) { cout<<"? "<<x+1<<" "<<y+1<<endl; int z; cin>>z; return z; } int main() { int n; cin>>n; int mx=0;
for(int i=1;i<n;i++) { int a=pos(mx,i); int b=pos(i,mx); if(a>b) { ans[mx]=a; mx=i; } else ans[i]=b; } ans[mx]=n; cout<<"!"; for(int i=0;i<n;i++) cout<<" "<<ans[i]; cout<<endl; return 0; }