当前位置:网站首页>CFdiv2-Fixed Point Guessing-(区间答案二分)
CFdiv2-Fixed Point Guessing-(区间答案二分)
2022-07-03 01:38:00 【可爱美少女】
题意:
就是给你一个1到n的数组,但是其中进行了n+1/2次交换,每个数只会被交换一次,所以只有一个数没有交换过。然后你可以查询15次,每次查询一个区间,会返回给你这个区间的数从小到大排列后是什么。现在问你哪一个数没有被交换。
思考:
刚开始看到15次就知道是二分,但是以为是dfs二分,每次递归判断。但是我感觉又不太好写。其实呢,对于二分,可以二分答案在哪个区间。也就是先判断l到r,如果答案在这里面,r = mid。否则l = mid+1。也就是在另一个区间。这就是单纯的判断在哪个区间,并没有答案越好越差啥的。但是怎么判呢,其实对于,输入的数x,如果x在l到r里面,那么意味着他是给这里面的换的,那么这里面的数就会出现两次,否则就是0次。但是哪个没有交换的数会贡献一次,所以如果是奇数,那么答案就是这个区间。
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 2e5+10,M = 2010;
int T,n,m,k;
int va[N];
int check(int l,int r)
{
cout<<"? "<<l<<" "<<r<<"\n";
cout.flush();
int sum = 0;
for(int i=l;i<=r;i++)
{
int x;
cin>>x;
if(x>=l&&x<=r) sum++;
}
return sum;
}
signed main()
{
cin>>T;
while(T--)
{
cin>>n;
int l = 1,r = n;
while(l<r)
{
int mid = (l+r)/2;
if(check(l,mid)&1) r = mid;
else l = mid+1;
}
cout<<"! "<<l<<"\n";
}
return 0;
}
总结:
多多思考,积累经验。
边栏推荐
- Network security - man in the middle attack
- Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
- DDL basic operation
- Types of map key and object key
- Analyzing several common string library functions in C language
- Learn BeanShell before you dare to say you know JMeter
- 技术大佬准备就绪,话题C位由你决定
- Query product cases - page rendering data
- 机器学习笔记(持续更新中。。。)
- stm32F407-------DMA
猜你喜欢

使用Go语言实现try{}catch{}finally

The technology boss is ready, and the topic of position C is up to you

Huakaiyun (Zhiyin) | virtual host: what is a virtual host

自定义组件、使用npm包、全局数据共享、分包

PS去除水印详解

stm32F407-------IIC通讯协议

What are the key points often asked in the redis interview
![[camera topic] how to save OTP data in user-defined nodes](/img/3e/b76c4d6ef9ab5f5b4326a3a8aa1c4f.png)
[camera topic] how to save OTP data in user-defined nodes

Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
![[data mining] task 3: decision tree classification](/img/9a/1cb2a8a90f16e7bfa9b48e516a966c.png)
[data mining] task 3: decision tree classification
随机推荐
Analysis, use and extension of open source API gateway apisex
Network security - Information Collection
stm32F407-------IIC通讯协议
【Camera专题】OTP数据如何保存在自定义节点中
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
Socket编程
Certaines fonctionnalités du développement d'applets
函数的定义和调用、this、严格模式、高阶函数、闭包、递归
[leetcode] 797 and 1189 (basis of graph theory)
2022 financial product revenue ranking
Network security - vulnerabilities and Trojans
Problems encountered in small program development of dark horse shopping mall
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
PS remove watermark details
stm32F407-------ADC
leetcode961. Find the elements repeated N times in the array with length 2n
[data mining] task 1: distance calculation
深度(穿透)选择器 ::v-deep/deep/及 > > >