有图可以直观发现,如果一开始的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. EasyUI Calendar 日历插件,只显示年月。

    从别人的博客园搬过来的,放在这里只是为了方便自己用.已经注明原文出处,尊重别人的劳动成果. 原文地址:http://www.cnblogs.com/hmYao/p/5779463.html 此日历插件 ...

  2. FD.io社区中国行暨未来网络技术沙龙·南京站 会议小结

    What is FD.io VPP? FD.io VPP(Fast Data Input/Output Vector Packet Processing)is a new network multi- ...

  3. 【Step By Step】将Dotnet Core部署到Docker下

    一.使用.Net Core构建WebAPI并访问Docker中的Mysql数据库 这个的过程大概与我之前的文章<尝试.Net Core—使用.Net Core + Entity FrameWor ...

  4. SQL Server 数据库每个表占用的空间、大小

    查看MSSQL数据库每个表占用的空间大小 sp_spaceused显示行数.保留的磁盘空间以及当前数据库中的表所使用的磁盘空间,或显示由整个数据库保留和使用的磁盘空间. 语法sp_spaceused ...

  5. FMDB数据库使用

    创建数据库路径 NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainM ...

  6. 网站jcms流程分析

    本实例大致流程:基于jsp页面,通过servlet传递数据调用方法,利用service更改数据库.本文重点分析的是其中的两个小方法add()和delete(),来反映出反射机制的一个具体作用:减少Se ...

  7. RHS 和 LHS

    不成功的的RHS 引用会导致抛出 ReferenceError异常 不成的的LHS 引用会导致自动隐式地创建一个全局变量(非严格模式下)   function foo(a) { var b = a; ...

  8. Hadoop相关笔记

    一.            Zookeeper( 分布式协调服务框架 ) 1.    Zookeeper概述和集群搭建: (1)       Zookeeper概述: Zookeeper 是一个分布式 ...

  9. MySQL学习之路(一)——初涉MySQL。

    MySQL学习之路(一) 1.1MySQL的概述 MySQL由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一个开源的关系型数据库管理系统. MySQL分为社区版和企业版. 1 ...

  10. Spring AOP代理模式

    代理模式 代理模式是一种设计模式,提供了对目标对象的另外的访问方式.即通过代理访问目标对象. 好处:可以再目标对象实现的基础上,增加额外的功能的操作.扩展目标对象的功能,而不改变现有的功能逻辑. 1. ...