跳到正文

个人记录

AT-Interactive Sorting

chinaxjh约 3 分钟
原文

这是 2026年2月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2020年1月25日
收录
2026年2月17日
更新
2026年8月25日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

题目链接

Interactive Sorting

前言

做过CF的交互题之后上AT来看看,试机题就有难度,思考了一下午

分析

Score:100

一开始看Sample,打了一个冒泡,稳稳拿了一个100

C++
#include<bits/stdc++.h>
using namespace std;
int n,q,i,j;
char s[30];
bool bo;
int main()
{
	scanf("%d%d",&n,&q);
	for (i=1;i<=n;i++)
	s[i]=(char)(i+64);
	for (i=1;i<=n;i++)
	{
		bo=true;
		for (j=1;j<=n-1;j++)
	  {
	  	printf("? %c %c\n",s[j],s[j+1]);
	  	fflush(stdout);
	  	char ans;
	  	scanf(" %c",&ans);
	  	if (ans=='>') 
		  {
		  	swap(s[j],s[j+1]);
		  	bo=false;
		  }
	  }
	  if (bo) break;
	}
	printf("! ");
	for (i=1;i<=n;i++)
	printf("%c",s[i]);
	printf("\n");
	fflush(stdout);
}

Score:200

想了一想,打了一个归并

C++
#include<bits/stdc++.h>
using namespace std;
int n,q,i;
char a[100],b[100];
void merge(int l,int r)
{
	int top;
	top=0;
	char ans;
	if (l<r)
	{
		int mid,x,y,i;
		mid=(l+r)/2;
		merge(l,mid);
		merge(mid+1,r);
		x=l;
		y=mid+1;
		while ((x<=mid)&&(y<=r))
		{
			printf("? %c %c\n",a[x],a[y]);
			fflush(stdout);
            scanf(" %c", &ans);
			if (ans=='>') 
			{
				top++;
				b[top]=a[y];
				y++;
			} else {
				top++;
				b[top]=a[x];
				x++;
			}
		}
		while (x<=mid)
		{
			top++;
			b[top]=a[x];
			x++;
		}
		while (y<=r)
		{
			top++;
			b[top]=a[y];
			y++;
		}
		for (i=l;i<=r;i++)
		a[i]=b[i-l+1];
	}
}
int main()
{
	cin>>n>>q;
	for (i=1;i<=n;i++)
	a[i]=(char)(i+64);
	merge(1,n);
	printf("! ");
	for (i=1;i<=n;i++)
	printf("%c",a[i]);
	printf("\n");
	fflush(stdout);
	return 0;
}

然后以为是稳稳的300,但是找了一下午终于找到了hack数据

代码
BECDA

ABCDE
+1
BACDE
+2
BCADE
+1
BCAED
+4
BECDA

于是针对数据继续思考

Score:300