当前位置:网站首页>Codeforces Round #803 (Div. 2) VP补题
Codeforces Round #803 (Div. 2) VP补题
2022-07-01 08:06:00 【sophilex】
大意:
有一个1-n的升序排序,从中选n-1/2对不相交的数字进行交换,会有一个数字是没有动的。
操作过后会得到一个最终序列。
交互问题。每次询问l,r,返回最终序列的l到r元素的升序排列
15个问题内找到没有改变位置的元素
思路:
做倒是做出来了,只是过程有点艰辛。。。(又是读不懂题的一天
n的范围有1e4那么大,但是只能问15个问题,差不多就是1/log的级别,那么自然就可以想到二分.
每次处理一个区间的话,就可以将区间分成两个部分,不妨先查左半部分,如果左半部分没问题,就意味着我们的答案在右半部分。
现在唯一的问题就是如何判断一个区间有没有问题了。
不妨定义一个元素对于一个区间是合法的,如果它的元素值满足val>=l&&val<=r。
现在考察我们的区间,在该区间内合法的元素,它只有两种情况:
1.它没有交换过,这个就是我们需要的答案。
2.它交换了,但是它仍在区间内,所以它是与区间内的另一个元素交换的,从这我们可以看出,交换过的合法元素一定是成对的。
所以:
如果当前区间内的合法数字有奇数个,那就一定是若干对交换的合法元素和一个答案,我们再对这个区间进行二分就可以了。如果有偶数个的话,那么久说明答案再另一个区间了。
最后区间范围变成1的时候,那个值就是答案。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=2e5+10;
ll n,m,k;
ll t;
ll a;
ll mas[N];
void cx(ll l,ll r)
{
if(l==r)
{
std::cout<<"? "<<l<<' '<<r<<endl;
cin>>a;
std::cout<<"! "<<a<<endl;
return;
}
ll mid=l+r>>1;
ll cnt=0;
std::cout<<"? "<<l<<" "<<mid<<endl;
for(int i=1;i<=mid-l+1;++i) std::cin>>mas[i];
for(int i=1;i<=mid-l+1;++i)
{
if(mas[i]>=l&&mas[i]<=mid) cnt++;
}
if(cnt%2)
{
cx(l,mid);
}
else cx(mid+1,r);
}
void solve()
{
std::cin>>n;
cx(1,n);
}
int main()
{//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
std::cin>>t;
while(t--)
{
solve();
}
return 0;
}(不知道为啥,总感觉交互题比普通题更有意思。。。)
边栏推荐
- Oracle create auto increment ID
- 【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
- Thesis learning -- Analysis and Research on similarity query of hydrological time series
- Conscience Amway universal wheel SolidWorks model material website
- 力扣每日一题-第32天-1822.数组元素积的符号
- Soft keyboard height error
- Latex table
- Software testing methods and techniques - overview of basic knowledge
- Find the nearest n-th power of 2
- SharePoint - modify web application authentication using PowerShell
猜你喜欢

Aardio - 阴影渐变文字
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

【入门】输入n个整数,输出其中最小的k个

Connect timed out of database connection

谈谈数字化转型的几个关键问题
![[MySQL learning notes 25] SQL statement optimization](/img/01/cf0556961670bb43512caab8d5f4e5.png)
[MySQL learning notes 25] SQL statement optimization

Significance and measures of source code encryption

The triode is a great invention

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
![[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)](/img/3e/75a1152f9cdf63c6779fdadec702a0.jpg)
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
随机推荐
Connect timed out of database connection
Gdip - hatchBrush图案表
Introduction to kubernetes resource objects and common commands (II)
web254
谈谈数字化转型的几个关键问题
Source code analysis of open source API gateway APIs IX
【入门】取近似值
Implementation and encapsulation of go universal dynamic retry mechanism
ContentType所有类型对比
Precautions and skills in using regular expressions in golang
[kv260] generate chip temperature curve with xadc
Cmake I two ways to compile source files
Sorting out tcp/udp communication problems
Significance and measures of source code encryption
Microsoft stream - how to modify video subtitles
slice扩容机制分析
uni 热更新
Office365 - how to use stream app to watch offline files at any time
web254
How outlook puts together messages with the same discussion