有图可以直观发现,如果一开始的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. Activation Functions

    Sigmoid Sigmoids saturate and kill gradients. Sigmoid outputs are not zero-centered. Exponential fun ...

  2. PAT——1056. 组合数的和

    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...

  3. oracle11g之Oracle体系结构(理论基础知识)

    第二章 oracle的体系结构 一.oracle体系结构概述1.实例和数据库组成完整的Oracle数据库系统数据库:一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等)实例:一组oracl ...

  4. 阅读Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks

    Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks Globlec ...

  5. ARP 协议 理解

    ARP协议的本质是使局域网内的其他主机能够知道我在哪儿,比如在局域网上有人冲着所有人喊了一句「IP为XXXX的家伙,你在哪儿」,我一听,XXXX不是我的IP吗,我得回答他啊,于是我冲着所有人(也可以是 ...

  6. sys_arch interface for lwIP 2.0.3

    sys_arch interface for lwIP 2.0.3 Author: Adam Dunkels Simon Goldschmidt The operating system emulat ...

  7. 书单list

    2018年对我来说是个踩到狗屎的一年,不能说运气差,只能说过去所有的不良决策后果都集中到2018年爆发了,希望新的一年,都好起来.书单很短,买的确实很多,真的惭愧. 昨日世界 南渡北归 上学记 回忆录 ...

  8. 并发编程(四)------并发quene

    在并发队列上JDK提供了两套实现,一个是以ConcurrentLinkedQueue为代表的高性能队列,一个是以BlockingQueue接口为代表的阻塞队列,无论哪种都继承自Queue接口! Con ...

  9. Annotation原理

    为什么使用注解: 1.通过注解,可保存源数据在Java源代码中:并且注解不同于注释,注解享有编译期的类型检查保护. 2.可在描述符性质的类或接口中抽取重复性的工作,通过注解来简化与自动化这些工作. 注 ...

  10. iOS开发Mac配置(CocoaPods、SourceTree、ssh key)

    作为开发,有一个自己的饭碗还是有必要的.因为交接旧电脑的时候,你会遇到了一些问题,而自己的电脑就方便很多了. 要开发,当然要装一些与开发相关的东西,那么新电脑入手,要做些什么呢? 1.安装Xcode: ...