hdu5323 Solve this interesting problem(爆搜)
2015-07-29 19:43
423 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Total Submission(s): 1731 Accepted Submission(s): 519
[/b]
[align=left]Problem Description[/align]
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 Lu≠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.
[/b]
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test, output one line contains one integer. If there is no such n, just output -1.
[align=left]Sample Input[/align]
6 7
10 13
10 11
[align=left]Sample Output[/align]
7
-1
12
比较隐晦的指出了爆搜的最大深度一定不会超过11层,所以,复杂度就是4^11次方,然后加一点可行性的剪枝之类,就能AC了
Solve this interesting problem
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1731 Accepted Submission(s): 519
[/b]
[align=left]Problem Description[/align]
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 Lu≠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.
[/b]
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test, output one line contains one integer. If there is no such n, just output -1.
[align=left]Sample Input[/align]
6 7
10 13
10 11
[align=left]Sample Output[/align]
7
-1
12
比较隐晦的指出了爆搜的最大深度一定不会超过11层,所以,复杂度就是4^11次方,然后加一点可行性的剪枝之类,就能AC了
//##################### //Author:fraud //Blog: http://www.cnblogs.com/fraud/ //##################### //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <sstream> #include <ios> #include <iomanip> #include <functional> #include <algorithm> #include <vector> #include <string> #include <list> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include <cctype> using namespace std; #define XINF INT_MAX #define INF 0x3FFFFFFF #define MP(X,Y) make_pair(X,Y) #define PB(X) push_back(X) #define REP(X,N) for(int X=0;X<N;X++) #define REP2(X,L,R) for(int X=L;X<=R;X++) #define DEP(X,R,L) for(int X=R;X>=L;X--) #define CLR(A,X) memset(A,X,sizeof(A)) #define IT iterator typedef long long ll; typedef pair<int,int> PII; typedef vector<PII> VII; typedef vector<int> VI; ll ans = 0; void dfs(ll l,ll r){ if(l<0)return; if(r<l)return; if(l==0){ if(ans==-1)ans = r; else ans = min(ans,r); return; } if(r>=ans&&ans!=-1)return; if((r-l+1)>(l))return; dfs(l-(r-l)-2,r); dfs(l,r+(r-l)); dfs(l-(r-l)-1,r); dfs(l,r+(r-l)+1); return; } int main() { ios::sync_with_stdio(false); ll l,r; while(cin>>l>>r){ ans = -1; dfs(l,r); cout<<ans<<endl; } return 0; }
相关文章推荐
- 黑马程序员——C语言--常见的内存分配函数
- Centos6.5 Python2.7+Supervisor 环境安装
- Solve this interesting problem(线段树逆二分模拟的DFS递归操作)
- 模板类的友元重载
- ArcGIS 10.1系列软件 下载
- java培训 2015-07-29
- 选项卡二
- 操作系统专题——进程与线程的区别
- AOP学习之Log4net
- EJB invocations from a remote client using JNDI
- 【峰回路转】Excel技巧百例 09.删除空白行
- HDOJ 1250 Hat's Fibonacci(大数,错了11次!!)
- sed用法小结
- 第3章 MFC原理介绍
- Geant4.10.01.p02例子学习-(basic)
- 第五篇 学习OpenCV之视频处理
- 远程登入开发板的几种方法(ssh,putty.exe,telnet,超级终端)
- 第五篇 学习OpenCV之视频处理
- hdu1863 最小生成树基础
- JS面向对象编程