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. [算法] 官方题 ...
随机推荐
- redis安装及测试
http://jingyan.baidu.com/article/9113f81b0333e12b3214c7a8.html 下载地址:http://git.oschina.net/bingoPure ...
- Shell笔记-04
如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. 举个例子: #!/bin/bash a=10 echo -e "Value ...
- H.264的码率控制:CBR和VBR
CBR: Constants Bits Rate, 静态比特率. 比特率在流的进行过程中基本保持恒定并且接近目标比特率,当对复杂内容编码时质量会下降. 在流式播放方案中使用CBR编码最为有效;优点是带 ...
- 【Step By Step】将Dotnet Core部署到Docker上
本教程的前提是,你已经在Linux服务器上已经成功的安装了Docker,我会大概介绍在此过程中用到的Docker命令,并不会介绍所有的Docker命令(因为我也不会). 一.在Docker中运行Dot ...
- 学习使用 ARM 的 math 库,据说 速度比C标准库 自带的 快 几十倍 到几百倍
1.首先 添加 库 到 工程 ,路径 如下 C:\Keil\ARM\CMSIS\Lib\ARM 2.包含头文件以及在 工程里 添加 头文件 路径如下 C:\Keil\ARM\CMSIS\Includ ...
- EF Core 2.0中如何手动映射数据库的视图为实体
由于Scaffold-DbContext指令目前还不支持自动映射数据库中的视图为实体,所以当我们想使用EF Core来读取数据库视图数据的时候,我们需要手动去做映射,本文介绍如何在EF Core中手动 ...
- java Clob类型 转String
1.我的数据库是oracle11g 遇到取出来的字段是clob类型,但是所需要的是string类型,写一个转换函数就可以解决问题了. // Clob类型 转String public String C ...
- jQuery实现导航栏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML5--Table
1.先给大加看下效果图,有点干劲 2.编写的代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 与Linux的第一次遭遇
我的与linux首次遭遇战 虚拟机安装 安装虚拟机我遇到的问题个数可以缩减到1--我几乎没遇到安装虚拟机的问题!我严格按照老师给的链接去下载那个VirtualBox,尽管那个网页是全英文的,但是我像看 ...