codeforces1407C Chocolate Bunny

https://codeforces.com/contest/1407/problem/C

Because it is a 1-n arrangement, then if pi%pj<pj%pi, then it must be pj=pj% pi, and compare it with two uncertain numbers each time, a total of 2*(n-1) queries You can find 1-n-1.

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

const int maxl=3e5+10;

int n,m,cnt,tot,cas,ans;
int a[maxl];
bool vis[maxl],numin[maxl];
char s[maxl];

inline void prework()
{
	scanf("%d",&n);
}

inline int print(int u,int v)
{
	int x;
	printf("? %d %d\n",u,v);
	fflush(stdout);
	scanf("%d",&x);
	return x;
}

inline void mainwork()
{
	if(n==1)
	{
		printf("! 1");
		fflush(stdout);
		return;
	}
	int t1,t2,last=1,now;
	for(int i=1;i<=n-1;i++)
	{
		for(int j=1;j<=n;j++)
		if(!vis[j] && last!=j)
		{
			now=j;
			break;
		}
		t1=print(last,now);
		t2=print(now,last);
		if(t1>t2)
		{
			vis[last]=true;numin[t1]=true;
			a[last]=t1;last=now;
		}
		else
		{
			vis[now]=true;numin[t2]=true;
			a[now]=t2;last=last;
		}
	}
	for(int i=1;i<=n;i++)
	if(!vis[i])
		a[i]=n;
	printf("!");
	for(int i=1;i<=n;i++)
		printf(" %d",a[i]);
	fflush(stdout);
}


int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();		
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/liufengwei1/article/details/108480944