CodeForces - 1480C Searching Local Minimum(交互+二分)
生活随笔
收集整理的這篇文章主要介紹了
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],且邊界分別滿足:
此時我們假如詢問出 midmidmid 和 mid+1mid+1mid+1 位置的值,無非只有兩種情況:
這樣一來新的區間的長度縮小了一半,且仍然滿足初始時的性質
這樣迭代到 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(交互+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1543D2
- 下一篇: CodeForces - 1480D1