hdu 5323 Solve this interesting problem(dfs)

题目链接:hdu 5323 Solve this interesting problem

逆向思维,每次向左或向右翻倍,知道左端点为0时,即恰好满足的情况,处理处所有情况去取最小值。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;

ll L, R, N;

void dfs(ll l, ll r) {
	//printf("%lld %lld\n", l, r);
	if (l <= 0 || r >= 2 * R) {
		if (l == 0)
			N = min(N, r);
		return;
	}

	int k = r - l + 1;

	if (k > l)
		return;

	dfs(l - k, r);
	dfs(l - k - 1, r);

	dfs(l, r + k);
	if (k > 1)
		dfs(l, r + k - 1);
}

int main () {
	while (scanf("%lld%lld", &L, &R) == 2) {
		N = inf;
		dfs(L, R);
		printf("%lld\n", N == inf ? -1 : N);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 04:04:38

hdu 5323 Solve this interesting problem(dfs)的相关文章

hdu 5323 Solve this interesting problem dfs 搜索

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5323 Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1470    Accepted Submission(s): 420 Problem Description Have you

HDU 5323 Solve this interesting problem

搜索... Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 422    Accepted Submission(s): 98 Problem Description Have you learned something about segment tree? If not,

HDU 5323 SOLVE THIS INTERESTING PROBLEM 爆搜

链接 Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 511    Accepted Submission(s): 114 Problem Description Have you learned something about segment tree? If not, d

【搜索】 HDU 5323 Solve this interesting problem

点击打开链接 用线段树方式建树 [ 0, n] 已知[ l, r] 结点 求n 若 建一个[0, 2*r] 的线段树  这是的总点数的奇的,(左子树!=右子树 [0, r]  在左子树里 则n最大为2*r 若 建一个[0, 2*r+1] 的线段树 (左子树==右子树 [0, r]  在左子树里 这时则 [0, r] 就可以建树 所以搜的时候超出2*r 就直接return #include <cstdio> #include <cstring> #include <cstdli

DFS+剪枝 HDOJ 5323 Solve this interesting problem

题目传送门 1 /* 2 题意:告诉一个区间[L,R],问根节点的n是多少 3 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - len -1,r]; 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <queue> 10

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

hdu 5349 MZL&#39;s simple problem(multiset)

代码: #include<set> #include<cstdio> using namespace std; multiset<int> st; int main() { int n; multiset<int>::iterator it; while(scanf("%d",&n)==1) { st.clear(); int k,num; for(int i=0; i<n; i++) { scanf("%d&qu

HDU 5349 MZL&#39;s simple problem(优先队列)

MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 776    Accepted Submission(s): 375 Problem Description A simple problem Problem Description You have a multiple set,and now