HDOJ 5323 Solve this interesting problem BFS搜索
来源:互联网 发布:淘宝被扣24分还能用吗 编辑:程序博客网 时间:2024/05/17 08:18
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 .
- IfLu=Ru , u is a leaf node.
- IfLu≠Ru , 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 valueLroot=0 and Rroot=n contains a node u with Lu=L and Ru=R .
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:
- If
- If
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
Input
The input consists of several test cases.
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤2015
Each test case contains two integers L and R, as described above.
Output
For each test, output one line contains one integer. If there is no such n, just output -1.
Sample Input
6 710 1310 11
Sample Output
7-112
Source
2015 Multi-University Training Contest 3
/* ***********************************************Author :CKbossCreated 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_left2if(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_right1if(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_right2if(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
- HDOJ 5323 Solve this interesting problem BFS搜索
- 【搜索】 HDU 5323 Solve this interesting problem
- hdu 5323 Solve this interesting problem dfs搜索
- hdu 5323 Solve this interesting problem dfs 搜索
- Hdu 5323 Solve this interesting problem(搜索)
- Solve this interesting problem
- HDU 5323 Solve this interesting problem
- HDU 5323 SOLVE THIS INTERESTING PROBLEM 爆搜
- HDU 5323 Solve this interesting problem(暴搜)
- hdu 5323 Solve this interesting problem 【DFS】
- hdu 5323 Solve this interesting problem
- HDU 5323 Solve this interesting problem(DFS)
- HDU 5323Solve this interesting problem(DFS)
- Hdu 5323 Solve this interesting problem【Dfs】
- H - Solve this interesting problem
- HDU5323.Solve this interesting problem
- HUD 5323 Solve this interesting problem dfs剪枝搜索,重构线段树
- hdu 5323 Solve this interesting problem(dfs)
- struts.xml的结构
- HDU 3501 Calculation 2(欧拉函数)
- 杭电1873 看病要排队
- 结构体运用
- hdu 4444 Walk(预处理+暴力)
- HDOJ 5323 Solve this interesting problem BFS搜索
- I - Pots -BFS
- poj 2135 有流量限制的最小费用最大流
- Android获取图片资源的4种方式
- 生命,因为热爱所以敬畏
- CSU1660(深搜)
- iOS 开发一些小技巧(收集整理)
- POJ2425 A Chess Game(博弈)
- 初识JavaScript