当前位置:网站首页>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;
}
总结:
多多思考,积累经验。
边栏推荐
- Swift development learning
- Network security ACL access control list
- Everything file search tool
- Types of map key and object key
- 网络安全-浅谈安全威胁
- Network security - dynamic routing protocol rip
- Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
- 网络安全-破解系统密码
- Network security - cracking system passwords
- Visual yolov5 format data set (labelme JSON file)
猜你喜欢
Asian Games countdown! AI target detection helps host the Asian Games!
深度学习笔记(持续更新中。。。)
[data mining] task 2: mimic-iii data processing of medical database
"Jetpack - livedata parsing"
MySQL学习03
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
【Camera专题】手把手撸一份驱动 到 点亮Camera
How is the mask effect achieved in the LPL ban/pick selection stage?
Machine learning notes (constantly updating...)
随机推荐
Depth (penetration) selector:: v-deep/deep/ and > > >
String replace space
Leetcode(540)——有序数组中的单一元素
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
ByteDance data Lake integration practice based on Hudi
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
How to deal with cache hot key in redis
Basic operation of view
【Camera专题】手把手撸一份驱动 到 点亮Camera
Network security - cracking system passwords
[camera topic] how to save OTP data in user-defined nodes
The technology boss is ready, and the topic of position C is up to you
网络安全-密码破解
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
Niuniu's ball guessing game (dynamic planning + prefix influence)
网络安全-漏洞与木马
【Camera专题】OTP数据如何保存在自定义节点中
DDL basic operation
使用Go语言实现try{}catch{}finally