Codeforces Round #503 (by SIS, Div. 2) D. The hat

有图可以直观发现,如果一开始的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的更多相关文章
- Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分
cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...
- 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 ...
- Codeforces Round #503 (by SIS, Div. 2)
连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...
- 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 ...
- Codeforces Round #503 (by SIS, Div. 2)-C. Elections
枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...
- Codeforces Round #503 (by SIS, Div. 1)E. Raining season
题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...
- Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)
题目大意:每个同学可以指定一个人,然后构成一个有向图.1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出. 思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实 ...
- 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 ...
- Codeforces Round #503 (by SIS, Div. 2) E. Sergey's problem
E. Sergey's problem [题目描述] 给出一个n个点m条边的有向图,需要找到一个集合使得1.集合中的各点之间无无边相连2.集合外的点到集合内的点的最小距离小于等于2. [算法] 官方题 ...
随机推荐
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- Python入门语法
Python入门语法 动态变量 a=3 整数 a='abc' a="abc" 字符串 a=3.0 小数 a=true a=false 布尔型 a=3 ...
- MAC升级openssl
Mac OSX EI Capitan 10.11.6升级自带Openssl - 简书 Mac10.11升级安装openssl _ 刘春桂的博客 openssl_openssl_ TLS_SSL and ...
- 浅谈 Virtual DOM 的那些事
背景 我们都知道频繁的dom给我们带来的代价是昂贵的,例如我们有时候需要去更新Table 的部分数据,必须去重新重绘表格,这代价实在是太大了,相比于频繁的手动去操作dom而带来性能问题,vdom很好的 ...
- 1005. Spell It Right(20)—PAT 甲级
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- 树莓派3B+SimpleCV上连接iPhone4s摄像头
目的:把iPhone4s当成网络摄像头,通过wifi连接到树莓派上,做为树莓派的摄像头. 1. iPhone4s上安装mini WebCam应用. 很旧的一个app, 没有密码,简单,无广告,免费. ...
- Python学习 :常用模块(三)----- 日志记录
常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...
- python财经数据接口包Tushare pro的入门及简单使用方式(大数据,股票数据接口)
最近在做一个项目,需要用到股票的数据,我在网上查了很久,最终发现在股票数据上面还是tushare比较专业,而且对于将来做金融行业的大数据这一块的,tushare绝对是你的一个好帮手,所以下面我就简单介 ...
- [转]Web登录中的信心安全问题
1. 一个简单的HTML例子看看用户信息安全 标准的HTML语法中,支持在form表单中使用<input></input>标签来创建一个HTTP提交的属性,现代的WEB登录中, ...
- java->php之在线子域名查询-接口光速版
因为不懂java,所以 用php重写了大佬的 在线子域名查询-接口光速版 http://sbd.ximcx.cn/ 这是大佬的 然后 改一下 ,用php 其实就是改了几行代码而已,jquery和aj ...