CodeForces - 1480C Searching Local Minimum(交互+二分)
題目鏈接:點(diǎn)擊查看
題目大意:給出一個(gè)長(zhǎng)度為 nnn 的排列,需要找出一個(gè)“局部最小值”,所謂“局部最小值”就是對(duì)于某個(gè) iii 來(lái)說(shuō),滿足 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
可以詢問(wèn)不超過(guò) 100100100 次,每次可以詢問(wèn)一個(gè)位置的值
題目分析:挺有意思的一道二分的變形,我們可以從 a0=an+1=infa_0=a_{n+1}=infa0?=an+1?=inf 這個(gè)位置入手,不難看出初始時(shí)的答案區(qū)間 [l,r][l,r][l,r] 對(duì)應(yīng)的就是 [1,n][1,n][1,n],且邊界分別滿足:
此時(shí)我們假如詢問(wèn)出 midmidmid 和 mid+1mid+1mid+1 位置的值,無(wú)非只有兩種情況:
這樣一來(lái)新的區(qū)間的長(zhǎng)度縮小了一半,且仍然滿足初始時(shí)的性質(zhì)
這樣迭代到 l==rl==rl==r 時(shí),肯定是滿足情況的一種解了
如此一來(lái)詢問(wèn)復(fù)雜度就轉(zhuǎn)換為二分的時(shí)間復(fù)雜度了,是 logloglog 級(jí)別的,大約不到 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; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1480C Searching Local Minimum(交互+二分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CodeForces - 1543D2
- 下一篇: CodeForces - 1480D1