Today, Bob plays with a child. There is a row of n

numbers. One can takes a number from the left side or the right side in turns and gets the grade which equals to the number. Bob knows that the child always chooses the bigger number of the left side and right side. If the number from two sides is equal, child will always choose the left one.
The child takes first and the person who gets more grade wins. The child will be happy only when he wins the game.

Bob wants to make the child happy, please help him calculate the minimal difference of their grades when he loses the game.

InputThere are T test cases (T≤2).

For each test case:

the first line only contains a number n (1≤n≤90&&n%2==0)

The second line contains n integers: a1,a2…an(1≤ai≤105).

OutputFor each test ease, you should output the minimal difference
of their grades when Bob loses the game. If Bob can't lose the game,
output "The child will be unhappy...".

Sample Input

4
2 1 5 3
2
2 2

Sample Output

5
The child will be unhappy...

Child每次取最大的,如果相等就取左边的;

那么我们可以记忆化搜索出[L,R]区间的按游戏规则的可以取到的最大值和最小值;
df 表示 Grade_bob - Grade_child 的值;
如果df+dpmin[L,R]>0,那么剪去;
如果df+dpmax[L,R]<=ans,剪去;
如果df+dpmax[L,R]<0,更新,return;
然后就是搜索了,
(卡时也是秀)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
int n;
int a[maxn];
int sum[maxn];
int dp1[200][200], dp2[200][200];
int ST;
int Lim = 0.00045 * CLOCKS_PER_SEC; int DP1(int l, int r) {
int &ans = dp1[l][r];
if (ans != -1)return ans;
if (l > r)return ans = 0;
if (a[l] >= a[r])
ans = min(DP1(l + 1, r - 1) + a[r], DP1(l + 2, r) + a[l + 1]);
else
ans = min(DP1(l + 1, r - 1) + a[l], DP1(l, r - 2) + a[r - 1]);
return ans;
} int DP2(int l, int r) {
int &ans = dp2[l][r];
if (ans != -1)return ans;
if (l > r)return ans = 0;
if (a[l] >= a[r]) {
ans = max(DP2(l + 1, r - 1) + a[r], DP2(l + 2, r) + a[l + 1]);
}
else ans = max(DP2(l + 1, r - 1) + a[l], DP2(l, r - 2) + a[r - 1]);
return ans;
}
int ans; void dfs(int l, int r, int df) {
if (l > r) {
ans = max(ans, df); return;
}
if (df + 2 * dp1[l][r] - (sum[r] - sum[l - 1]) >= 0)return;
if (df + 2 * dp2[l][r] - (sum[r] - sum[l - 1]) <= ans)return;
if (df + 2 * dp2[l][r] - (sum[r] - sum[l - 1]) < 0) {
ans = max(ans, df + 2 * dp2[l][r] - (sum[r] - sum[l - 1]));
return;
}
if (clock() - ST > Lim)return;
if (a[l] >= a[r]) {
dfs(l + 1, r - 1, df + a[r] - a[l]);
dfs(l + 2, r, df + a[l + 1] - a[l]);
}
else {
dfs(l + 1, r - 1, df + a[l] - a[r]);
dfs(l, r - 2, df + a[r - 1] - a[r]);
}
}
int main()
{
// ios::sync_with_stdio(0); while (cin >> n) {
ms(a); ms(sum);
for (int i = 1; i <= n; i++)a[i] = rd(), sum[i] = sum[i - 1] + a[i]; ST = clock();
mclr(dp1, -1); mclr(dp2, -1);
DP1(1, n); DP2(1, n);
ans = -inf;
dfs(1, n, 0);
ans = abs(ans);
if (ans >= inf) {
puts("The child will be unhappy...");
}
else printf("%d\n", ans);
}
return 0;
}

hdu 6196 搜索+剪枝的更多相关文章

  1. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

  3. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  4. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  5. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

  6. NOIP2015 斗地主(搜索+剪枝)

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 270  Solved: 192[Submit][Status] ...

  7. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  8. luogu 1731 搜索剪枝好题

    搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...

  9. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

随机推荐

  1. Python基础学习七 网络编程

    主要应用urllib和requests模块 urllib模块返回类型为bytes,需要数据类型转换:requests就方便很多. 例子1:发送post请求 url = 'http://api.nnzh ...

  2. 负载均衡LVS之DR(附:NFS共享存储) 五分钟搭建手册

    一:DR-模式 调度器Ip:192.168.1.254 服务器ip:192.168.1.1/192.168.1.2 VIP:192.168.1.10 ——配置负载均衡调度器: 关闭重定向响应配置: V ...

  3. ASP.NET MVC 和 WebForm的权限控制

    今天主要讲一下对于ASP.NET的页面级权限控制 数据结构:用户表.角色表.权限表.角色权限派生表 为用户添加权限的数据配置后, 自定义类对MVC继承Controller 对其内置方法Initiali ...

  4. Codeforces 919F——A Game With Numbers

    转自大佬博客:https://www.cnblogs.com/NaVi-Awson/p/8405966.html; 题意 两个人 Van♂ 游戏,每人手上各有 8'>88 张牌,牌上数字均为 [ ...

  5. 【总结整理】pv、uv

    1.pv的全称是page view,译为页面浏览量或点击量,通常是衡量一个网站甚至一条网络新闻的指标.用户每次对网站中的一个页面的请求或访问均被记录1个PV,用户对同一页面的多次访问,pv累计.例如, ...

  6. Log4J 配置 详解

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...

  7. 关于Rest Framework中View、APIView与GenericAPIView的对比分析

    关于Rest Framework中View.APIView与GenericAPIView的对比分析  https://blog.csdn.net/odyssues_lee/article/detail ...

  8. 防止SQL注入方法总结

    一.参数化SQL 是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) 来给值,用@来表示参数. 在使用参数化查询的情况下,数据库服务器不会将参数的内容视为 ...

  9. Recurrent Neural Network(递归神经网络)

    递归神经网络(RNN),是两种人工神经网络的总称,一种是时间递归神经网络(recurrent neural network),另一种是结构递归神经网络(recursive neural network ...

  10. Python htmlTestRunner生成测试报告Demo

    #该代码段是ReadTxt_demo.py 的代码,用户读取txt 文件中的用户信息. #ReadTxt_demo.py def readTxt(filePath): fo = open(filePa ...