有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同

那么中间必然存在一个答案

简单总结就是大小关系不同,中间就有答案

所以就可以使用二分

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std; int main() {
int n;
while(~scanf("%d", &n)) {
printf("? 1\n");
fflush(stdout);
int t1; scanf("%d", &t1); printf("? %d\n", 1 + n/2);
fflush(stdout);
int t2; scanf("%d", &t2); bool flag = t1 > t2;
if(t1 == t2) {
printf("! 1\n");
fflush(stdout);
continue;
} if(n == 2) {
printf("! -1\n");
fflush(stdout);
continue;
} int l = 1; int r = 1+n/2; bool success = false;
while(l < r - 1) {
int mid = (l + r) >>1;
int mid2 = mid + n/2; printf("? %d\n", mid);
fflush(stdout);
int t1; scanf("%d", &t1); printf("? %d\n", mid2);
fflush(stdout);
int t2; scanf("%d", &t2); if(t1 == t2) {
success = true;
printf("! %d\n", mid);
fflush(stdout);
break;
}
if( (t1 < t2) ^ flag) l = mid;
else r = mid;
} if(!success) {
printf("! -1\n");
fflush(stdout);
}
}
return 0;
}

Codeforces Round #503 (by SIS, Div. 2) D. The hat的更多相关文章

  1. Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分

    cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...

  2. Codeforces Round #503 (by SIS, Div. 2) Solution

    从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat P ...

  3. Codeforces Round #503 (by SIS, Div. 2)

    连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...

  4. Codeforces Round #503 (by SIS, Div. 2) C. Elections (暴力+贪心)

    [题目描述] Elections are coming. You know the number of voters and the number of parties — n and m respe ...

  5. Codeforces Round #503 (by SIS, Div. 2)-C. Elections

    枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...

  6. Codeforces Round #503 (by SIS, Div. 1)E. Raining season

    题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...

  7. Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)

    题目大意:每个同学可以指定一个人,然后构成一个有向图.1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出. 思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实 ...

  8. Codeforces Round #503 (by SIS, Div. 2) C. Elections(枚举,暴力)

    原文地址 C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #503 (by SIS, Div. 2) E. Sergey's problem

    E. Sergey's problem [题目描述] 给出一个n个点m条边的有向图,需要找到一个集合使得1.集合中的各点之间无无边相连2.集合外的点到集合内的点的最小距离小于等于2. [算法] 官方题 ...

随机推荐

  1. C#中Array类

    Array类是C#中所有数组的基类,它是在System命名空间中定义的,Array类提供了各种用于数组的属性和方法

  2. NIO高并发基础

    NIO高并发 是jdk1.4出现的新的流. NIO - New IO - 同步式非阻塞式IO BIO - Blocking IO - 同步式阻塞式IO ---UDP/TCP ==AIO - Async ...

  3. 分布式架构学习-Consul集群配置

    简介 之前公司用的是Consul进行服务发现以及服务管理,自己一直以来只是用一下,但是没有具体的深入,觉得学习不可以这样,所以稍微研究了一下. 网上有很多关于Consul的介绍和对比,我这里也不献丑了 ...

  4. ios开发UI篇—UISlider

    概述 UISlider用于从连续范围的值中选择单个值的控件. 当您移动滑块的大拇指时,会将其更新后的值传递给附加的任何动作.滑块的外观是可配置的; 您可以对曲目和大拇指进行着色,并提供出现在滑块末端的 ...

  5. PHP+jQuery实现双击修改table表格

    <td signs="name"> <input type="text" disabled="disabled" read ...

  6. shell习题第6题:监听80端口

    [题目要求] 写一个脚本,判断本机的80端口(加入服务为httpd)是否开启,如果开启就什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知相关人员 [核心要点] 检测80端口使用 ...

  7. 树莓3B+_root密码开启

    开启root用户的方法:1.设置密码:sudo passwd2.sudo passwd --unlock root3.root用户登录:su

  8. exynos4412—CMU裸板复习

    本章描述了Exynos 4412 SCP的时钟管理单元(CMUs).在Exynos 4412 SCP中,CMUs控制相位锁相环(PLLs),并为CPU.总线和单个ip的功能时钟生成系统时钟.它们还与电 ...

  9. python3 实现一个多级菜单小功能

    记录下一下 #!/usr/bin/env python3 ''' 需求:三级菜单 三级菜单,依次进入子菜单 ''' City = { '北京':{ '大兴区':[ '亦庄','黄村','中信新城',' ...

  10. anaconda安装包找不到

    Anaconda作为一个工具包集成管理工具,下载python工具包是很方便的,直接敲: conda install package_name 1 但是有时候安装一个工具包(如skimage)的时候,在 ...