原题:

Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

This is an interactive task.

There are N balls labeled with the first N uppercase letters. The balls have pairwise distinct weights.

You are allowed to ask at most Q queries. In each query, you can compare the weights of two balls (see Input/Output section for details).

Sort the balls in the ascending order of their weights.

Constraints

  • (N,Q)=(26,1000)(26,100), or (5,7).

Partial Score

There are three testsets. Each testset is worth 100 points.

  • In testset 1, N=26 and Q=1000.
  • In testset 2, N=26 and Q=100.
  • In testset 3, N=5 and Q=7.

Input and Output

First, you are given N and Q from Standard Input in the following format:

N Q

Then, you start asking queries (at most Q times). Each query must be printed to Standard Output in the following format:

? c1 c2

Here each of c1 and c2 must be one of the first N uppercase letters, and c1 and c2 must be distinct.

Then, you are given the answer to the query from Standard Input in the following format:

ans

Here ans is either < or >. When ans is <, the ball c2 is heavier than the ball c1, and otherwise the ball c1 is heavier than the ball c2.

Finally, you must print the answer to Standard Output in the following format:

! ans

Here ans must be a string of length N, and it must contain each of the first N uppercase letters once. It must represent the weights of the balls in the ascending order.

Judgement

  • After each output, you must flush Standard Output. Otherwise you may get TLE.
  • After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.
  • When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give WA).

题目大意:

这是一道交互题。有$N$个小球,用前$N$个大写字母编号,每个小球有不同的重量。

最多允许询问$Q$次。每次询问可以比较两个小球的重量。

按升序输出小球的质量。

一共有三组数据(子任务),每组数据100分。数据规模如下:

子任务编号 $N$ $Q$
$1$ $26$ $1000$
$2$ $26$ $100$
$3$ $5$ $7$

注意事项:

  1. 输出一次就要刷新一次标准输出,否则可能会被误判为TLE;
  2. 输出答案后,程序必须立即退出,否则judger的行为未定义;
  3. 若输出答案不符合格式或不正确,judger的行为未定义(不一定给出WA)。

题解:

第一次做交互题,感觉很新鲜,但是不知道该怎么做。首先程序要与交互库进行交互,根据交互库给出的信息判断小球的质量情况。

对于第一个子任务,由于$Q$的范围很大,直接询问$26^2$次进行一次选择排序就可以了。

对于第二个子任务,由于询问次数只有$100$次,我们假设数组已经有序,二分一个小球质量的位置为$mid$,然后与这个位置的小球的质量进行比较,进行一次插入排序即可。需要询问$26\log_226$次,还是不能通过所有的数据。这就要求我们需要对每一次询问的答案做一个记忆化处理,记录下询问小球的质量关系,然后根据这个质量关系就可以得出有序的数列,从而降低时间复杂度,当然如果不记忆化也有可能能够通过这道题目($123$次询问只比限制大一点)。

也可以写一个归并排序之类的东西。当然需要记忆化,记忆化每一个数大于哪些数小于哪些数(或者只记忆化比这个数大的最小的数与比这个数小的最大的数)。

对于第三个子任务,注意到这组数据非常特殊,其实与给$5$个小球,最多称$7$次求出质量关系的问题相同。可以自己思考一下询问次数最小的方案。

#include<bits/stdc++.h>
using namespace std; char s[29], ans[29];
int cmp['Z'+5]['Z'+5];
int cnt = 0, QQ = 1; bool cmp_user(const char a, const char b) {
char cp;
if(cmp[a][b]==-1) {
printf("? %c %c\n", a, b);
fflush(stdout);
scanf(" %c", &cp);
if(cp=='>') {
cmp[a][b] = true;
cmp[b][a] = false;
return true;
}
else {
cmp[a][b] = false;
cmp[b][a] = true;
return false;
}
}
else return cmp[a][b];
} void ins2(char c) {
if(cmp_user(c, s[1])) {
if(cmp_user(c, s[2])) s[3] = c;
else s[3] = s[2], s[2] = c;
} else {
if(cmp_user(c, s[0])) {
s[3] = s[2];
s[2] = s[1];
s[1] = c;
} else {
s[3] = s[2];
s[2] = s[1];
s[1] = s[0];
s[0] = c;
}
}
} void ins(char c) {
int l = 0, r = cnt;
while(l<r) {
int mid = l+r>>1;
if(cmp_user(c, ans[mid])) l = mid+1;
else r = mid;
}
cnt ++;
if(cmp_user(c, ans[r])) r ++;
for(int i=cnt; i>=r+1; i--) ans[i] = ans[i-1];
ans[r] = c;
} int main() {
int N, Q;
scanf("%d%d", &N, &Q); for(int i=0; i<26; i++) s[i] = (char)(i+'A');
s[N] = '\0'; if(N==26) {
memset(cmp, -1, sizeof(cmp));
cnt = 0;
ans[0] = s[0];
ans[N] = '\0';
for(int i=1; i<N; i++) ins(s[i]); printf("! %s\n", ans);
} else {
memset(cmp, -1, sizeof(cmp));
if(cmp_user(s[0], s[1])) swap(s[0], s[1]);
if(cmp_user(s[2], s[3])) swap(s[2], s[3]);
if(cmp_user(s[1], s[3])) {
swap(s[0], s[2]);
swap(s[1], s[3]);
}
char x = s[2];
if(cmp_user(s[4], s[1])) {
if(cmp_user(s[4], s[3])) {
s[2] = s[3];
ins2(x);
} else {
s[2] = s[4];
s[4] = s[3];
ins2(x);
}
} else {
if(cmp_user(s[4], s[0])) {
s[2] = s[1];
s[1] = s[4];
s[4] = s[3];
ins2(x);
} else {
s[2] = s[1];
s[1] = s[0];
s[0] = s[4];
s[4] = s[3];
ins2(x);
}
}
printf("! %s\n", s);
}
return 0;
}

  

非传统题初探——AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting)的更多相关文章

  1. 【刷题】AtCoder Regular Contest 003

    A.GPA計算 题意:\(n\) 个人,一个字符串表示每个人的等第,每种等第对应一种分数.问平均分 做法:算 #include<bits/stdc++.h> #define ui unsi ...

  2. 【刷题】AtCoder Regular Contest 002

    A.うるう年 题意:判断闰年 做法:.. #include<bits/stdc++.h> #define ui unsigned int #define ll long long #def ...

  3. 【刷题】AtCoder Regular Contest 001

    A.センター採点 题意:给一个只包含1.2.3.4的字符串,求出现次数最多和最少的字符 做法:还能怎么做... #include<bits/stdc++.h> #define ui uns ...

  4. AtCoder Beginner Contest 068 ABCD题

    A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...

  5. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  6. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  7. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  8. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. 飘逸的python - 实现glob style pattern

    一说起通配符,大家非常快就会想起*和? 号,有了通配符,使得表达能力大大增强,非常多linux命令都支持这个东西,事实上就是glob style pattern. 就连redis的keys命令都支持g ...

  2. GPS-Graph Processing System 改动源代码经验总结 (四)

    HamaWhite原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 本文目的:在改动GPS源代码后,具体描写叙述怎样编译和分发到各Worker节点上. 以下以Graph ...

  3. 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第七节:PS基础:UV动画

    <使用Cocos2d-x 开发3D游戏>系列在线课程 第七节:PS基础:UV动画 视频地址:http://edu.csdn.net/course/attend/1330/20807 交流论 ...

  4. Skyline V6.6.1安装文件下载及使用

     1.下载地址:http://www.skylineglobe.com/skylineglobe/corporate/download/DownloadCenter.aspx 2.安装指南:   ...

  5. Java类集-list

    Collection 子接口: ArrayList是List 接口和Collection接口的一个子类,用于实例化两种接口 package leiji; import java.util.ArrayL ...

  6. DataGuard总体结构

    一.DataGuard总体结构 总体目标 1.   描述计划和非计划停机的不同因数 2.   DataGuard的主要组件 3.   物理以及逻辑DataGuard的异同 4.   建立DataGua ...

  7. leetcode矩阵与动态规划相关

    目录 54/59螺旋矩阵 62不同路径 64最小路径和 120三角形最小路径和 695岛屿的最大面积 547朋友圈 718最长重复数组 221最大正方形 121/122/123/714/188买卖股票 ...

  8. leetcode数组相关

    目录 4寻找两个有序数组的中位数 11盛最多水的容器,42接雨水 15三数之和,16最接近的三数之和,18四数之和 26/80删除排序数组中的重复项, 27移除元素 31下一个排列 53最大子序和 5 ...

  9. (Go)10.流程控制示例

    package main import ( "math/rand" "fmt" ) func main() { //var n int n := rand.In ...

  10. C# Event.ClickCount 解决垃圾鼠标带来的烦恼

    今天调试遇到个Bug,百思不得其解的是在自己的设备上重来不重现,在测试机上百分百重现,如下: 问题:点击一次Button执行两次Click操作 分析:看Log的确是执行了两次,就像真的点击了两次But ...