• [思维]Tournament Countdown Codeforces1713D


    This is an interactive problem.

    There was a tournament consisting of 2n2n contestants. The 11-st contestant competed with the 22-nd, the 33-rd competed with the 44-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament.

    You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers aa and bb, which are the indices of two contestants. The jury will return 11 if aa won more matches than bb, 22 if bb won more matches than aa, or 00 if their number of wins was equal.

    Find the winner in no more than ⌈13⋅2n+1⌉⌈13⋅2n+1⌉ queries. Here ⌈x⌉⌈x⌉ denotes the value of xx rounded up to the nearest integer.

    Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries.

    Input

    The first line contains a single integer tt (1≤t≤2141≤t≤214) — the number of test cases.

    The only line of input contains a single integer nn (1≤n≤171≤n≤17).

    It is guaranteed that the sum of 2n2n over all test cases does not exceed 217217.

    Interaction

    The interaction for each test case begins by reading the integer nn.

    To make a query, output "? a b" (1≤a,b≤2n1≤a,b≤2n) without quotes. Afterwards, you should read one single integer — the answer for your query. You can make at most ⌈13⋅2n+1⌉⌈13⋅2n+1⌉ such queries in each test case.

    If you receive the integer −1−1 instead of an answer or a valid value of nn, it means your program has made an invalid query, has exceed the limit of queries, or has given incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

    When you are ready to give the final answer, output "! x" (1≤x≤2n1≤x≤2n) without quotes — the winner of the tournament. Giving this answer does not count towards the limit of queries. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.

    After printing a query or the answer do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

    • fflush(stdout) or cout.flush() in C++;
    • System.out.flush() in Java;
    • flush(output) in Pascal;
    • stdout.flush() in Python;
    • see documentation for other languages.

    Hacks

    To hack, use the following format.

    The first line contains an integer tt (1≤t≤2141≤t≤214) — the number of test cases.

    The first line of each test case contains a single integer nn (1≤n≤171≤n≤17).

    The second line of each test case contains 2n2n numbers on a line — the number of wins of each participant. There should be a sequence of matches that is consistent with the number of wins.

    The sum of 2n2n should not exceed 217217.

    Example

    input

    1
    3
    
    2
    
    0
    
    2
    
    

    output

    ? 1 4
    
    ? 1 6
    
    ? 5 7
    
    ! 7

    题意: 有2^n个选手参与锦标赛,最下层按照1~2^n排列,具体的输赢已经确定,现在你可以询问a和b,程序将告诉你谁胜利次数多,如何在2^(n+1)/3次询问内找到最终的胜者。

    分析: 2^(n+1)/3也就是(2/3)*2^n,一共会进行2^n-1次比赛,所以需要减少1/3的询问数,也就是对于这样的三场比赛只用两次询问得到结果:

    而具体实现也很简单,先询问1和3,然后假如是1获胜的次数多,那就再问1和4,而如果1和3胜利次数相同,那就可以问2和4,通过这样的方式就能知道这颗子树的最终胜者。所以可以在这个满二叉树上递归处理得到答案。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. int tr[1<<20];
    8. signed main()
    9. {
    10. int T;
    11. cin >> T;
    12. while(T--){
    13. int n;
    14. scanf("%d", &n);
    15. int num = 1<
    16. for(int i = 1; i <= num; i++)
    17. tr[i+num-1] = i;
    18. int now = n+1, res;
    19. while(now > 1){
    20. if(now == 2){
    21. printf("? %d %d\n", tr[2], tr[3]);
    22. fflush(stdout);
    23. scanf("%d", &res);
    24. if(res == 1) tr[1] = tr[2];
    25. else tr[1] = tr[3];
    26. break;
    27. }
    28. for(int i = 1<<(now-2-1); i <= (1<<(now-2))-1; i++){
    29. int t1 = tr[i<<2], t2 = tr[(i<<1|1)<<1];
    30. printf("? %d %d\n", tr[i<<2], tr[(i<<1|1)<<1]);
    31. fflush(stdout);
    32. scanf("%d", &res);
    33. if(res == 0){
    34. printf("? %d %d\n", tr[i<<2|1], tr[(i<<1|1)<<1|1]);
    35. fflush(stdout);
    36. scanf("%d", &res);
    37. if(res == 1) tr[i] = tr[i<<2|1];
    38. else tr[i] = tr[(i<<1|1)<<1|1];
    39. }
    40. else if(res == 1){
    41. printf("? %d %d\n", tr[i<<2], tr[(i<<1|1)<<1|1]);
    42. fflush(stdout);
    43. scanf("%d", &res);
    44. if(res == 1) tr[i] = tr[i<<2];
    45. else tr[i] = tr[(i<<1|1)<<1|1];
    46. }
    47. else{
    48. printf("? %d %d\n", tr[i<<2|1], tr[(i<<1|1)<<1]);
    49. fflush(stdout);
    50. scanf("%d", &res);
    51. if(res == 1) tr[i] = tr[i<<2|1];
    52. else tr[i] = tr[(i<<1|1)<<1];
    53. }
    54. }
    55. now -= 2;
    56. }
    57. printf("! %d\n", tr[1]);
    58. fflush(stdout);
    59. }
    60. return 0;
    61. }
    62.  

  • 相关阅读:
    收集域内DNS信息
    wpf datagrid常用绑定以及格式
    fft分析-ADC/DAC
    【原创】win11完美搭建macos13实现IOS自动化测试环境
    node 第十八天 中间件express-session实现会话密钥
    uniapp安卓 华为商店 vivo商店 oppo 小米 上架问题 Android中怎么才能不提前申请权限
    VCED:AI能用文字自动剪切视频?|datawhale开源项目
    ERP采购管理 华夏
    敏捷开发的特点及敏捷工具
    【工具篇】Unity翻书效果的三种方式
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126850754