您的位置:首页 > 其它

hdu5323 Solve this interesting problem(爆搜)

2015-07-29 19:43 423 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: