Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分
题意:
交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同。数列保证相邻的两个差值为1或-1;
思路:
构造函数f(x) = a[x] - a[x + n/2] ,由于a数列差值为1或-1,所以可以发现f(x)是连续的。然后就可以用二分了,这种二分的check方式是自己第一次见的。就是通过f(mid)和f(d)的正负来判断区间的移动。其中d是任选的。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
const int maxn = 1e5+;
ll f[maxn];
int n;
void get(int m){
int x,y;
// cout<<"? "<<m<<endl;
printf("? %d\n",m);
fflush(stdout);
scanf("%d", &x); // cout<<"? "<<m+n/2<<endl;
// cin>>y;
printf("? %d\n",m + n/);
fflush(stdout);
scanf("%d", &y);
f[m] = y - x;
}
int main(){ // cin>>n;
scanf("%d", &n);
int le = , ri = n/;
get();
if(f[]==){
printf("! 1\n");
fflush(stdout);
}
while(le<=ri){
int mid = (le + ri) / ;
get(mid);
if(f[mid]==){
printf("! %d\n",mid);
fflush(stdout);
return ;
}
if(f[]>){
if(f[mid]>){
le = mid+;
}
else ri = mid - ;
}
else {
if(f[mid]<){
le = mid+;
}
else ri = mid - ;
}
}
printf("! -1\n");
fflush(stdout); return ;
}
cf1020D
Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分的更多相关文章
- Codeforces Round #503 (by SIS, Div. 2) D. The hat
有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #includ ...
- Codeforces Round #503 (by SIS, Div. 2)
连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...
- 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) 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. [算法] 官方题 ...
随机推荐
- 【Maven】Mac 使用 zsh 后 mvn 命令就无效
RT -- 解决方法: 打开 .zshrc 文件,将 Maven 环境变量配置加入其中,或者 将 source ~/.bash_profile 添加到 .zshrc 中. PS: 之前搞不懂,每次使用 ...
- 【Java】字符串空格相关
1. 去掉首尾空格 [trim() 方法] String.trim() //去掉首尾空格 2. 替换多个空格为一个 [replaceAll() 方法] str.replaceAll(" + ...
- 【iOS】PrefixHeader.pch
还不太理解,暂且记下.
- Dubbo源码学习之-服务导出
前言 忙的时候,会埋怨学习的时间太少,缺少个人的空间,于是会争分夺秒的工作.学习.而一旦繁忙的时候过去,有时间了之后,整个人又会不自觉的陷入一种懒散的状态中,时间也显得不那么重要了,随便就可以浪费掉几 ...
- win7 python pdf2image入坑经历
Python开发菜鸟入坑 项目要求pdf转成图片,网上较多的方案对于windows极其不友好,wand,Pythonmagick(win下载地址:www.lfd.uci.edu/~gohlke/pyt ...
- eclipse导入码云-GIT项目
1.首先找到项目源码地址我随便找到一个git地址 :https://gitee.com/mingSoft/MCMS 2.打开eclipse空白处右键导入项目搜索git. 3.将第一步复制的git地址复 ...
- docker-compose 综合训练
Docker-compose综合训练 一. 实验目的: 熟悉Docker-compose的基本工作原理: 熟悉Docker-compose安装 熟悉Docker compose命令基础 熟悉Docke ...
- SonarQube系列二、分析dotnet core/C#代码
[前言] 本系列主要讲述sonarqube的安装部署以及如何集成jenkins自动化分析.netcore项目.目录如下: SonarQube系列一.Linux安装与部署 SonarQube系列二.分析 ...
- 通过注解实现通用导出Excel
Javaweb开发中数据的导入导出很常见,每次我们都需要写很多代码,所以我就在想能不能写一些通用的方法,之前已经在网上 整理过一些通用的方法,最近在网上看到一位牛人封装的更加简介,自己拿过来整理了一下 ...
- HashMap源码分析之面试必备
今天我们就面试会问到关于HashMap的问题进行一个汇总,以及对这些问题进行解答. 1.HashMap的数据结构是什么? 2.为啥是线程不安全的? 3.Hash算法是怎样实现的? 4.HashMa ...