AT-Interactive Sorting

· · 个人记录

题目链接

Interactive Sorting

前言

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

分析

Score:100

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

#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

想了一想,打了一个归并

#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