当前位置:网站首页>CFdiv2-Fixed Point Guessing-(區間答案二分)
CFdiv2-Fixed Point Guessing-(區間答案二分)
2022-07-03 02:04: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;
}
總結:
多多思考,積累經驗。
边栏推荐
- Ni visa fails after LabVIEW installs the third-party visa software
- DML Foundation
- 力扣(LeetCode)183. 从不订购的客户(2022.07.02)
- Modify table structure
- leetcode961. Find the elements repeated N times in the array with length 2n
- [error record] navigator operation requested with a context that does not include a naviga
- String replace space
- Anna: Beibei, can you draw?
- Explore the conversion between PX pixels and Pt pounds, mm and MM
- Network security - cracking system passwords
猜你喜欢

Network security - vulnerabilities and Trojans

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

詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信

自定义组件、使用npm包、全局数据共享、分包
![[leetcode] 797 and 1189 (basis of graph theory)](/img/2a/9c0a904151a17c2d23dea9ad04dbfe.jpg)
[leetcode] 797 and 1189 (basis of graph theory)

Visual yolov5 format data set (labelme JSON file)

Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly

详细些介绍如何通过MQTT协议和华为云物联网进行通信

A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?

Learn BeanShell before you dare to say you know JMeter
随机推荐
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[camera topic] complete analysis of camera dtsi
Stm32f407 ------- IIC communication protocol
Network security - password cracking
详细些介绍如何通过MQTT协议和华为云物联网进行通信
《上市风云》荐书——唯勇气最可贵
全链路数字化转型下,零售企业如何打开第二增长曲线
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
MySQL learning 03
Basic operation of view
Take you ten days to easily complete the go micro service series (II)
Reprint some Qt development experience written by great Xia 6.5
小程序开发黑马购物商城中遇到的问题
深度(穿透)选择器 ::v-deep/deep/及 > > >
网络安全-中间人攻击
Redis:Redis的简单使用
机器学习笔记(持续更新中。。。)
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
技术大佬准备就绪,话题C位由你决定