转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Solve this interesting problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1731 Accepted Submission(s): 519
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 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.
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=ncontains 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: 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.
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=ncontains 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.
0≤L≤R≤109
LR−L+1≤2015
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤2015
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
比较隐晦的指出了爆搜的最大深度一定不会超过11层,所以,复杂度就是4^11次方,然后加一点可行性的剪枝之类,就能AC了
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 //#pragma comment(linker, "/STACK:102400000,102400000") 6 #include <iostream> 7 #include <sstream> 8 #include <ios> 9 #include <iomanip> 10 #include <functional> 11 #include <algorithm> 12 #include <vector> 13 #include <string> 14 #include <list> 15 #include <queue> 16 #include <deque> 17 #include <stack> 18 #include <set> 19 #include <map> 20 #include <cstdio> 21 #include <cstdlib> 22 #include <cmath> 23 #include <cstring> 24 #include <climits> 25 #include <cctype> 26 using namespace std; 27 #define XINF INT_MAX 28 #define INF 0x3FFFFFFF 29 #define MP(X,Y) make_pair(X,Y) 30 #define PB(X) push_back(X) 31 #define REP(X,N) for(int X=0;X<N;X++) 32 #define REP2(X,L,R) for(int X=L;X<=R;X++) 33 #define DEP(X,R,L) for(int X=R;X>=L;X--) 34 #define CLR(A,X) memset(A,X,sizeof(A)) 35 #define IT iterator 36 typedef long long ll; 37 typedef pair<int,int> PII; 38 typedef vector<PII> VII; 39 typedef vector<int> VI; 40 ll ans = 0; 41 void dfs(ll l,ll r){ 42 if(l<0)return; 43 if(r<l)return; 44 if(l==0){ 45 if(ans==-1)ans = r; 46 else ans = min(ans,r); 47 return; 48 } 49 if(r>=ans&&ans!=-1)return; 50 if((r-l+1)>(l))return; 51 dfs(l-(r-l)-2,r); 52 dfs(l,r+(r-l)); 53 dfs(l-(r-l)-1,r); 54 dfs(l,r+(r-l)+1); 55 return; 56 } 57 int main() 58 { 59 ios::sync_with_stdio(false); 60 ll l,r; 61 while(cin>>l>>r){ 62 ans = -1; 63 dfs(l,r); 64 cout<<ans<<endl; 65 } 66 return 0; 67 }