HDOJ 5323 Solve this interesting problem BFS搜索

51 篇文章 0 订阅


BFS暴力搜索.....

Solve this interesting problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 974    Accepted Submission(s): 263


Problem Description
Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:
- For each node u in Segment Tree, u has two values:  Lu  and  Ru .
- If  Lu=Ru , u is a leaf node. 
- If  LuRu , u has two children x and y,with  Lx=Lu , Rx=Lu+Ru2 , Ly=Lu+Ru2+1 , Ry=Ru .
Here is an example of segment tree to do range query of sum.



Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node's value  Lroot=0  and  Rroot=n contains a node u with  Lu=L  and  Ru=R .
 

Input
The input consists of several test cases. 
Each test case contains two integers L and R, as described above.
0LR109
LRL+12015
 

Output
For each test, output one line contains one integer. If there is no such n, just output -1.
 

Sample Input
  
  
6 7 10 13 10 11
 

Sample Output
  
  
7 -1 12
 

Source
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年07月28日 星期二 22时00分45秒
File Name     :HDOJ5323.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;
typedef pair<LL,LL> pII;

const LL INF = (1LL<<60);

LL L,R;
LL ans;

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(cin>>L>>R)
	{
		ans=INF;

		queue<pII> q;
		q.push(make_pair(L,R));

		while(!q.empty())
		{
			pII u = q.front(); q.pop();


			if(u.second>ans) continue;

			if(u.first==0)
			{
				ans=min(ans,u.second);
				continue;
			}

			LL deta = u.second-u.first+1;

			if(deta>u.first) continue;

			LL right = u.first-1;
			LL left1 = right-deta+1; 
			LL left2 = right-deta; 

			/// push_left1 

			if(left1>=0&&u.second>=0&&left1<=u.second)
			{
				pII v;
				v.first=left1; v.second=u.second;
				LL D=v.second-v.first+1;
				if(D>v.first&&v.first) ;
				else q.push(v);
			}

			/// push_left2

			if(left2>=0&&u.second>=0&&left2<=u.second)
			{
				pII v;
				v.first=left2; v.second=u.second;
				LL D=v.second-v.first+1;
				if(D>v.first&&v.first) ;
				else q.push(v);
				
			}

			LL left=u.second+1;
			LL right1=left+deta-1;
			LL right2=left+deta-2;

			/// push_right1

			if(u.first>=0&&right1>=0&&u.first<=right1&&right1<ans)
			{
				pII v;
				v.first=u.first; v.second=right1;
				LL D=v.second-v.first+1;
				if(D>v.first&&v.first) ;
				else q.push(v);
			}

			/// push_right2

			if(u.first>=0&&right2>=0&&u.first<=right2&&right2<ans)
			{
				pII v;
				v.first=u.first; v.second=right2;
				LL D=v.second-v.first+1;
				if(D>v.first&&v.first) ;
				else q.push(v);
				
			}

		}

		if(ans==INF) ans=-1;
		cout<<ans<<endl;
	}

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值