当前位置:
首页 >
CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互)
发布时间:2024/4/11
45
豆豆
生活随笔
收集整理的这篇文章主要介绍了
CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
题目链接:点击查看
题目分析:给出一个长度为 nnn 的序列,现在可以进行最多 202020 次查询,每次查询可以询问区间 [l,r][l,r][l,r] 中次大值的位置,现在要求在查询后输出最大值的位置
题目分析:C1C1C1 的 404040 次查询是比较简单的二分,就不多说了,关于本题,个人感觉更像是一道思维偏重的构造题,直接说做法吧
可以先利用一次查询,询问 [1,n][1,n][1,n] 的次大值位置,记为 ppp
再用一次查询,询问最大值是在 [1,p−1][1,p-1][1,p−1] 还是 [p+1,n][p+1,n][p+1,n] 之中
这里假设最大值在 [p+1,n][p+1,n][p+1,n] 之中,那么我们发现,假设最大值的位置为 ttt,我们对于下面三种询问讨论:
似乎满足了二分的性质?这样我们就可以二分去寻找最大值的位置了,询问复杂度为 O(logn)+2=19O(logn)+2=19O(logn)+2=19
代码:
// Problem: C2. Guessing the Greatest (hard version) // Contest: Codeforces - Codeforces Round #703 (Div. 2) // URL: https://codeforces.com/contest/1486/problem/C2 // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int query(int l,int r) {if(l>=r) {return -1;}printf("? %d %d\n",l,r);fflush(stdout);int x;scanf("%d",&x);return x; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;scanf("%d",&n);int pos=query(1,n),ans=-1;if(pos==query(1,pos)) {//[1,pos]int l=1,r=pos;while(l<=r) {int mid=(l+r)>>1;if(query(mid,pos)==pos) {l=mid+1;ans=mid;} else {r=mid-1;}}} else {//[pos,n]int l=pos,r=n;while(l<=r) {int mid=(l+r)>>1;if(query(pos,mid)==pos) {r=mid-1;ans=mid;} else {l=mid+1;}}}printf("! %d\n",ans);return 0; }总结
以上是生活随笔为你收集整理的CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: CodeForces - 1486B E
- 下一篇: CodeForces - 1486D M