[haut] 1281: 邪能炸弹 dp
题目描述
输入
对于测试实例:
第一行为三个整数 n,begin,MAX。1<=n<=50,0<=begin<=MAX,1<=MAX<=1000。
第二行一次为n个整数 x1,x2,x3,x4...xn。1<=xi<=1000
输出
样例输入
2
3 5 10
5 3 7 3 3 8
5 2 10
样例输出
10
-1 思路:当时的写的是dfs ac了 可以构造一颗搜索树 然后搜索就好了
题解是用的dp 设dp[i][j]为第i天时值为j的存在状态 1为存在 0为不存在 如果dp[i-1][j] = 1 那么可由此推出dp[i][j + Xi] 和 dp[i][j - Xi]的存在状态
若j在i-1和i阶段取值0~Max内都不存在的话 直接可以返回-1了
dfs:
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
int a[];
int n, b, Max;
int ans; void dfs(int ret, int t)
{
if (ret < || ret > Max)
return;
if (t == n) {
ans = max(ans, ret);
return;
}
dfs(ret+a[t], t+);
dfs(ret-a[t], t+);
} int main()
{
//freopen("1.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
ans = -;
scanf("%d%d%d", &n, &b, &Max);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
dfs(b, );
printf("%d\n", ans);
} return ;
}
dp:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[][];
int n, b, Max;
int x[]; int main()
{
//freopen("1.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &b, &Max);
for (int i = ; i <= n; i++)
scanf("%d", &x[i]);
memset(dp, , sizeof(dp));
dp[][b] = ;
int flag;
for (int i = ; i <= n; i++) {
flag = ;
for (int j = ; j <= Max; j++) {
if (dp[i-][j]) {
if (j + x[i] <= Max) {
dp[i][j + x[i]] = ;
flag = ;
}
if (j - x[i] >= ) {
dp[i][j - x[i]] = ;
flag = ;
}
}
}
if (flag == ) break;
}
int ans = -;
for (int i = Max; i >= ; i--)
if (dp[n][i]) {
ans = i;
break;
}
printf("%d\n", ans);
} return ;
}
[haut] 1281: 邪能炸弹 dp的更多相关文章
- 河南省多校联盟二-C
1281: 邪能炸弹 时间限制: 1 秒 内存限制: 128 MB提交: 222 解决: 80 题目描述 正在入侵艾泽拉斯的古尔丹偶然间得到了一颗邪能炸弹,经过研究,他发现这是一颗威力极其巨大且难 ...
- poj2228 Naptime【(环结构)线性DP】
Naptime Time Limit: 1000MS Memory Limit: 65536K Total Submissions:3374 Accepted: 1281 Descriptio ...
- HDU 5366 dp 递推
The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 51nod 1281山峰和旗子
1281 山峰和旗子 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 用一个长度为N的整数数组A,描述山峰和山谷的高度.山峰需要满足 ...
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
随机推荐
- 自定义mysql函数时报错,[Err] 1418 - This function has none of DETERMINISTIC......
今天在我执行自定义mysql函数的SQL时发生了错误,SQL如下: /** 自定义mysql函数 getChildList */delimiter //CREATE FUNCTION `pengwif ...
- Sass、Less和Stylus
1.背景介绍 1.Sass背景介绍 Sass是对CSS(层叠样式表)的语法的一种扩充,诞生于2007年,最早也是最成熟的一款CSS预处理器语言,它可以使用变量.常量.嵌套.混 入.函数等功能,可以更有 ...
- JS和OC间的通信(使用JavaScriptCore)
JavaScriptCore 时代的通讯 iOS 7 开始,苹果提供了一个叫作 JavaScriptCore 的框架,使用 JavaScriptCore 框架可以实现 OC 和 JS 的互相调用,而不 ...
- js查看对象内容
function show_obj(obj){ var temp,p1Str=""; for(temp in obj){ p1Str=p1Str+temp+":" ...
- [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?
3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...
- 2014.10.1 Word技巧
设置每页都出现的表头 wordDoc.Tables[tab].Rows[1].HeadingFormat = (int)Word.WdConstants.wdToggle; //合并单元格 wordD ...
- Ubuntu下设置VNCServer
Ubuntu下设置VNCServer Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作.服 ...
- eclipse和myeclipse的下载地址
官方下载地址: Eclipse 标准版 x86 http://mirror.hust.edu.cn/eclipse//technology/epp/downloads/release/luna/R/e ...
- Android CTS
1.什么是CTS CTS是google制定的兼容性测试包(Compatibility Test Suite),只有通过CTS测试的设备才有可能获得Android的商标和享受Android Market ...
- DAY17-Django之logging
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': ...