HDU 5323(2015多校3)-Solve this interesting problem(dfs+剪枝),hdu5323
題目地址:HDU 5323
題意:給一個l,r,表示區間[l,r],問是否存在區間為[0,n]的線段樹的節點區間為[l,r],如果有求最小的n,如果沒有輸出-1。
思路:因為L/(R-L+1)<=2015,按照線段樹的性質每次分區間序號擴大兩倍,所以可以得出差不多有22層,所以用爆搜就可以,由上把[l,r]區間不斷擴張,直到滿足l==0為止。順便剪剪枝。
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-8;int ans;int dfs(int l,int r){ if(l<0) return ans;//控制左邊界 if(!l){//儲存當前最小的右邊界 ans=min(ans,r); return ans; } if(2*l<r+1) return ans;//剪枝。相當於控制右邊界 if(l==r) {//左邊界等於右邊界的時候不用處理,直接輸出、 return r; } //下面是四種情況。 dfs(l,2*r-l+1); dfs(l,2*r-l); dfs(2*l-r-1,r); dfs(2*l-r-2,r);}int main(){ int l,r; while(~scanf("%d %d",&l,&r)){ ans=inf; ans=dfs(l,r); if(ans==inf) puts("-1"); else printf("%d\n",ans); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。