当前位置:网站首页>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;
}
总结:
多多思考,积累经验。
边栏推荐
- Trial setup and use of idea GoLand development tool
- His experience in choosing a startup company or a big Internet company may give you some inspiration
- 可視化yolov5格式數據集(labelme json文件)
- Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
- 疫情当头,作为Leader如何进行团队的管理?| 社区征文
- DDL basic operation
- What are MySQL locks and classifications
- Query product cases - page rendering data
- Certaines fonctionnalités du développement d'applets
- Analysis, use and extension of open source API gateway apisex
猜你喜欢

小程序開發的部分功能

【Camera专题】OTP数据如何保存在自定义节点中

Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol

Asian Games countdown! AI target detection helps host the Asian Games!

His experience in choosing a startup company or a big Internet company may give you some inspiration

机器学习笔记(持续更新中。。。)

What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it

Machine learning notes (constantly updating...)

mysql

stm32F407-------ADC
随机推荐
Network security ACL access control list
[data mining] task 3: decision tree classification
His experience in choosing a startup company or a big Internet company may give you some inspiration
Where is the future of test engineers? Confused to see
DML Foundation
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
Anna: Beibei, can you draw?
Leetcode(540)——有序数组中的单一元素
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
深度(穿透)选择器 ::v-deep/deep/及 > > >
One of the C language practical projects is greedy snake
Cloud native topic sorting (to be updated)
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
网络安全-密码破解
ByteDance data Lake integration practice based on Hudi
技术大佬准备就绪,话题C位由你决定
Basic operation of view
[Yu Yue education] Jiujiang University material analysis and testing technology reference
The testing process that software testers should know
[error record] navigator operation requested with a context that does not include a naviga