DFS+剪枝 HDOJ 5323 Solve this interesting problem

题目传送门

 /*
题意:告诉一个区间[L,R],问根节点的n是多少
DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r];
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std; typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll INFL = 1e30;
ll ans; void DFS(ll l, ll r) {
if (l > r || l < ) return ;
if (l == ) {
ans = min (ans, r); return ;
}
ll len = r - l + ;
if (l < len) return ;
DFS (l, r + len);
if (len != ) DFS (l, r + len - );
DFS (l - len, r); DFS (l - len - , r);
} int main(void) { //HDOJ 5323 Solve this interesting problem
//freopen ("H.in", "r", stdin); ll L, R;
while (scanf ("%I64d%I64d", &L, &R) == ) {
if (L == ) {
if (R >= L) printf ("%I64d\n", R);
else puts ("-1");
continue;
}
ans = INFL; DFS (L, R);
printf ("%I64d\n", (ans == INFL) ? - : ans);
} return ;
}
上一篇:cocos2dx 2.x 竖屏


下一篇:linux dev目录