HDU 5323(2015多校3)-Solve this interesting problem(dfs+剪枝),hdu5323

來源:互聯網
上載者:User

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;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.