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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1480C Searching Local Minimum(交互+二分)

發布時間:2024/4/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1480C Searching Local Minimum(交互+二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 nnn 的排列,需要找出一個“局部最小值”,所謂“局部最小值”就是對于某個 iii 來說,滿足 ai<ai?1a_i<a_{i-1}ai?<ai?1?ai<ai+1a_i<a_{i+1}ai?<ai+1?,更具體的, a0=an+1=infa_0=a_{n+1}=infa0?=an+1?=inf

可以詢問不超過 100100100 次,每次可以詢問一個位置的值

題目分析:挺有意思的一道二分的變形,我們可以從 a0=an+1=infa_0=a_{n+1}=infa0?=an+1?=inf 這個位置入手,不難看出初始時的答案區間 [l,r][l,r][l,r] 對應的就是 [1,n][1,n][1,n],且邊界分別滿足:

  • a0>a1=ala_0>a_1=a_la0?>a1?=al?
  • an<an+1=ara_n<a_{n+1}=a_ran?<an+1?=ar?
  • 此時我們假如詢問出 midmidmidmid+1mid+1mid+1 位置的值,無非只有兩種情況:

  • amid>amid+1a_{mid}>a_{mid+1}amid?>amid+1?:此時可以令原本的區間 [l,r][l,r][l,r] 的左端點右移,即新的區間變為 [mid+1,r][mid+1,r][mid+1,r]
  • amid<amid+1a_{mid}<a_{mid+1}amid?<amid+1?:此時可以令原本的區間 [l,r][l,r][l,r] 的右端點左移,即新的區間變為 [l,mid][l,mid][l,mid]
  • 這樣一來新的區間的長度縮小了一半,且仍然滿足初始時的性質

    這樣迭代到 l==rl==rl==r 時,肯定是滿足情況的一種解了

    如此一來詢問復雜度就轉換為二分的時間復雜度了,是 logloglog 級別的,大約不到 404040

    代碼:

    // Problem: C. Searching Local Minimum // Contest: Codeforces - Codeforces Round #700 (Div. 2) // URL: https://codeforces.com/contest/1480/problem/C // Memory Limit: 512 MB // Time Limit: 2000 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> #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 ask(int x) {printf("? %d\n",x);fflush(stdout);int ans;scanf("%d",&ans);return ans; } 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 l=1,r=n;while(l<r) {int mid=(l+r)>>1;int a=ask(mid),b=ask(mid+1);if(a>b) {l=mid+1;} else if(a<b) {r=mid;}}printf("! %d\n",l);fflush(stdout);return 0; }

    總結

    以上是生活随笔為你收集整理的CodeForces - 1480C Searching Local Minimum(交互+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。