当前位置:网站首页>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;
}
總結:
多多思考,積累經驗。
边栏推荐
- Method of removing webpage scroll bar and inner and outer margins
- [Yu Yue education] Jiujiang University material analysis and testing technology reference
- Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
- Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
- Swift开发学习
- 小程序開發的部分功能
- [leetcode] 797 and 1189 (basis of graph theory)
- 微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
- [fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
- 苏世民:25条工作和生活原则
猜你喜欢
Performance test | script template sorting, tool sorting and result analysis
自定义组件、使用npm包、全局数据共享、分包
Trial setup and use of idea GoLand development tool
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
mysql
詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
Analysis, use and extension of open source API gateway apisex
【Camera专题】Camera dtsi 完全解析
[camera topic] turn a drive to light up the camera
随机推荐
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
In the face of difficult SQL requirements, HQL is not afraid
Network security OpenVAS
easyPOI
技术大佬准备就绪,话题C位由你决定
Custom components, using NPM packages, global data sharing, subcontracting
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
Network security - cracking system passwords
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
Network security - Trojan horse
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
How is the mask effect achieved in the LPL ban/pick selection stage?
Network security - vulnerabilities and Trojans
查询商品案例-页面渲染数据
Depth (penetration) selector:: v-deep/deep/ and > > >
[AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
Huakaiyun (Zhiyin) | virtual host: what is a virtual host
Where is the future of test engineers? Confused to see
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires