日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互)

發(fā)布時(shí)間:2024/4/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:點(diǎn)擊查看

題目分析:給出一個(gè)長(zhǎng)度為 nnn 的序列,現(xiàn)在可以進(jìn)行最多 202020 次查詢,每次查詢可以詢問(wèn)區(qū)間 [l,r][l,r][l,r] 中次大值的位置,現(xiàn)在要求在查詢后輸出最大值的位置

題目分析:C1C1C1404040 次查詢是比較簡(jiǎn)單的二分,就不多說(shuō)了,關(guān)于本題,個(gè)人感覺更像是一道思維偏重的構(gòu)造題,直接說(shuō)做法吧

可以先利用一次查詢,詢問(wèn) [1,n][1,n][1,n] 的次大值位置,記為 ppp

再用一次查詢,詢問(wèn)最大值是在 [1,p?1][1,p-1][1,p?1] 還是 [p+1,n][p+1,n][p+1,n] 之中

這里假設(shè)最大值在 [p+1,n][p+1,n][p+1,n] 之中,那么我們發(fā)現(xiàn),假設(shè)最大值的位置為 ttt,我們對(duì)于下面三種詢問(wèn)討論:

  • [p,t?1][p,t-1][p,t?1]:返回的值肯定不是 ppp
  • [p,t][p,t][p,t]:返回的一定是 ppp
  • [p,t+1][p,t+1][p,t+1]:返回的也是 ppp
  • 似乎滿足了二分的性質(zhì)?這樣我們就可以二分去尋找最大值的位置了,詢問(wèn)復(fù)雜度為 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; }

    總結(jié)

    以上是生活随笔為你收集整理的CodeForces - 1486C2 Guessing the Greatest (hard version)(二分+交互)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。