hdu 6196 搜索+剪枝 原创 fish04 2022-05-27 20:36:35 博主文章分类:dp ©著作权 文章标签 #include #define ide 文章分类 后端开发 ©著作权归作者所有:来自51CTO博客作者fish04的原创作品,请联系作者获取转载授权,否则将追究法律责任 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 Input42 1 5 322 2Sample Output5The 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#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include//#include//#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-5typedef pair pii;#define pi acos(-1.0)//const int N = 1005;#define REP(i,n) for(int i=0;i<(n);i++)typedef pair 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;} EPFL - Fighting 赞 收藏 评论 分享 举报 上一篇:[SDOi2012]Longge的问题 BZOJ2705 数学 下一篇:没有上司的舞会 树形dp 提问和评论都可以,用心的回复会被更多人看到 评论 发布评论 全部评论 () 最热 最新 相关文章 GaussDB(for MySQL)剪枝功能,让查询性能提升70倍! 如何通过MySQL提升DISTINCT,尤其是多表连接下DISTINCT的查询效率? MySQL 表连接 执行效率 DISTINCT SQL语句 Elasticsearch深入搜索之“多字段”搜索 1. 最佳字段 假设有个网站允许用户搜索博客的内容,以下面两篇博客内容文档为例:PUT /my_index/my_type/1{ "title": "Quick brown rabbits", "body": "Brown rabbits are commonly seen."}PUT /my_index/my_type/2{ "title": "Keeping 搜索 召回率 Elasticsearch 国内AI搜索引擎推荐(一)秘塔AI搜索 今天给大家带来一款目前还比较小众,但是有着全新搜索体验的工具——秘塔AI搜索!它主打的口号就是没有广告,直达搜索! 搜索引擎 AI hdu 6196 搜索+剪枝 #include #define ide hdu 搜索+剪枝 剪枝是c[i] < ( n*m-cnt+1)/2 ,减掉很多没必要的搜索#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX 7using namespace std;int a[MAX][MAX];int c C++ 搜索 剪枝 i++ #include hdu1455 Sticks(搜索+剪枝+剪枝+.....+剪枝) SticksTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K hdu1455 hdu 1455 搜索+剪枝 #include i++ 【HDU 6171】Admiral(搜索+剪枝) "多校10 1001 HDU 6171 Admiral" 题意 目标状态是第i行有i+1个i数字(i=0~5)共6行。给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个。求20步以内到目标状态的最少步数是多少。 题解 设计一个估价函数来剪枝,每个数最少需要|a[i][j] i|步回到自己 bfs HDU 5113 Black And White(搜索+剪枝) 题意:给你一个不超过5X5的矩阵,有k种颜色,每种颜色有c[i]个,问可不可以把;int mp[6][6];int color i++ sed ci hdu 4090(搜索+可行性剪枝) 解题思路:这道题一开始我想用bfs,但这道用dfs+剪枝。。关键还是位置移动的部分没有完全弄明白。。。#include#i 搜索 i++ #include ios HDU 6196 happy happy happy (2017沈阳网赛 - 搜索 + dp + [黑科技。。。]) 题意:儿子和爸爸选牌, 每一次每个人只能从最左边选择或者在最右边选择, 儿子的决策是 选左边 和 右边最大的那个位置, 如果一样大, 选择左边, 爸爸的决策是为了让儿子赢, 问你 如果儿子能赢 爸爸与儿子的最小分数差是多少, 如果无论如和 爸爸都赢儿子 输出The child will be unhappy...思路 HDU 搜索 dp ide #include 连连看 HDU - 1175_搜索_剪枝 hdu有毒,考试上 AC 的就是一直 WA…其实这道题是可以进行初始化来进行优化的,这样的话询问次数是可以达到 10510^5105 的。不过普通的 dfsdfsdfs + 剪枝也是可过的。Code:#include#include#includeusing... 数学 HDU 4848 Wow! Such Conquering! (记忆化搜索,剪枝) Wow! Such Conquering!Time Limit: 15000/8000 MS (Java/Others) i++ #include Java poj 2078(搜索+剪枝) 解题思路:可以一行一行地递归求解,要 搜索 i++ #include ios hdu 1692(枚举+剪枝) 题意:给你一些井的信息,井中原有 dp #include i++ ios poj 1948(搜索+剪枝) 解题思路:这道题看到数据量,想到ncludeusing names 搜索 #include ios ci POJ - 1011 搜索剪枝 POJ2392和这题很类似~~只是这题更加强悍些~~~除了2392里要注意的搜索方式以及搜索顺序从大的到小外还有更多要注意的.. blog c 搜索 sed #include HDU 6005 Pandaland——dijkstra + 剪枝 #include #include #include #include #include #include #include #include using namesp, n, cnt, ans, d[maxn], #include i++ ios HDU 6005 Pandaland(dijkstra + 剪枝) 题意:给你一个m 个边的无向图,要求在图上找一个最小的环(边权)。思路:好暴 dijkstra ci #include Java HDU 4027 单点更新+“剪枝” 可以看出一个数开方大于等于7次时就一直为1了,利用这点剪枝#include#include#includetypedef long long ll;ll num[100010];struct Tree{ int lt id){ tree[id].l=s; tree query build struct #include HDU 2437 Jerboas (剪枝搜索) 题意:给定一幅图,图上有两种点T,P.......一只跳鼠在一个T点作为起始点,它想通过图上的路到达某个P点,P点满足如下要求:(1).到达P点的途中路径权值为k的倍数(2).尽量让路径权值取最小(3).权值相同时 #include i++ 权值 记忆化搜索 ios impala 读取hive表 1. 什么是ImpalaCloudera公司开源提供的一款sql on hadoop的软件。号称是当前大数据领域大查询最快的一款sql on hadoop的工具impala能够兼容hive,具有实时批处理等特点,提供高并发2. Impala与hive之间的关系impala与hive是紧耦合的,使用impala之前必须安装好hive数据仓库impala的优点以及缺点: 优点:计算速度很快,提供近乎实 impala 读取hive表 Impala 短路读取 hive hadoop 远程kvm上传镜像速度 目录1、基本操作指令2、vm删除与恢复3、设置vm自启动4、vm克隆5、vm快照本文包括KVM虚拟化操作中的: 基本命令操作、虚拟机删除与恢复、虚拟机自启动、虚拟机克隆、虚拟机镜像操作1、基本操作指令[root@linux-node1 /]# virsh shutdown CentOS-7.1-x86_64 域 CentOS-7.1-x86_64 被关闭[root@linux-node1 /]# 远程kvm上传镜像速度 KVM 虚拟化 CentOS linux ibdata1文件删除后mysql无法启动 mysql 误删除ibdata1之后如何恢复如果误删除了在线服务器中mysql innodb相关的数据文件ibdata1以及日志文件 ib_logfile*,应该怎样恢复呢?这时候应该一身冷汗了吧?==================================先抽根烟,冷静一下。==================================再观察一下网站,发现一切都很正常,数据的读取与 mysql ibdata1 删除 mysql 数据 文件复制 redis安装包for RAM 目录1 redis之列表2 redis之hash3 redis其他操作4 redis 管道5 django中使用redis5 celery介绍和安装6 celery快速使用7 celery包结构# 1 登录注册前端 -登录 -手机验证码登录---》输入框输入手机号---》监听失去焦点事件---》手机号正则校验(js),查询手机号是否存在----》发送验证码的按钮可以点击---》点击发 redis安装包for RAM redis hg 验证码 ingress 动态自定义配置 目录一、DHCP简介二、DHCP作用及特点三、DHCP服务器IP分配三种方式四、DHCP协议中的报文五、DHCP服务工作流程第一步:搜索阶段第二步:提供阶段第三步:选择阶段第四步:确认阶段六、安装和配置DHCP服务七、DHCP中继代理一、DHCP简介DHCP(Dynamic Host Configuration Protocol),动态主机配置协议,是一个应用层协议。当我们将客户主机ip地址设置为 ingress 动态自定义配置 DHCP IP 服务器