HDU 5543 Pick The Sticks
背包变形。与普通的背包问题不同的是:允许有两个物品可以花费减半。
因此加一维即可,dp[i][j][k]表示前i个物品,有j个花费减半了,总花费为k的情况下的最优解。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} int T,n,L;
LL f[][][];
struct X{int a;LL v;}s[]; int main()
{
scanf("%d",&T); int cas=; while(T--)
{
scanf("%d%d",&n,&L); L=L*; LL ans=;
for(int i=;i<=n;i++)
{
scanf("%d%lld",&s[i].a,&s[i].v);
s[i].a=s[i].a*;
ans=max(ans,s[i].v);
}
memset(f,,sizeof f); int p=;
for(int i=;i<=n;i++)
{
p=p^;
for(int j=;j<=;j++)
{
for(int c=;c<=L;c++)
{
f[p][j][c]=f[p^][j][c];
if(c-s[i].a>=) f[p][j][c]=max(f[p][j][c],f[p^][j][c-s[i].a]+s[i].v);
if(j->=&&c-s[i].a/>=) f[p][j][c]=max(f[p][j][c],f[p^][j-][c-s[i].a/]+s[i].v);
ans=max(ans,f[p][j][c]);
}
}
}
printf("Case #%d: %lld\n",cas++,ans);
}
return ;
}
HDU 5543 Pick The Sticks的更多相关文章
- hdu 5543 Pick The Sticks(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:给你一根长为m的长木板和一些小木棒,每一根小木棒有它的长度和价值,这些小木棒要放在长木板上 ...
- HDU 5543 Pick The Sticks:01背包变种
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意: 给你N个金条和一张长度为L的桌子.每个金条长度为a[i],价值为w[i].金条只能在桌子 ...
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
Pick The Sticks Time Limit: 15000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- 2015南阳CCPC D - Pick The Sticks dp
D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...
- CDOJ 1218 Pick The Sticks
Pick The Sticks Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- 2015南阳CCPC D - Pick The Sticks 背包DP.
D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...
- [HDOJ5543]Pick The Sticks(DP,01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...
- HDOJ5543 Pick The Sticks
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题目大意:有n个金条,每个金条有长度和价值,给一个长度为L的容器,当金条在容器两端的时候,只要重 ...
- HDU 1051:Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- echarts x轴或y轴文本字体颜色改变
1:x轴文本字体颜色改变 xAxis : [ { type : 'category', data : ['<30','30-','40-','50-','60-','>=70'], axi ...
- uitabbarController tababr 上方横线隐藏
在自定义的tabbarController里加上这一句就ok [[UITabBar appearance] setShadowImage:[UIImage new]]; [[UITabBar appe ...
- 用户输入密码隐藏之getpass的使用
有的时候,比如商城登录的时候,我希望输入的时候我的密码不为明文,如何实现呢? 这里就需要利用getpass模块中的getpass方法.注意,需要在linux上或者windows下运行,在pycharm ...
- 关于PHP的一小段代码求解如下求解"%2\$s"
<?php$format = "The %2\$s contains %1\$d monkeys";printf($format, 8, "北京");?& ...
- php 5.5 xhprof for windows
今天看到一个好的性能优软件xhprof(有facebook开发的类库)在国内找了很多网站都没有找到相关php5.5的扩展,只好FQ还是外面的世界精彩一下就找到了link (http://windows ...
- ZOJ 1926 Guessing Game
#include<cstdio> ],s2[]; ]; ]; int bz; int main() { int n,i; while(~scanf("%d",& ...
- JQ中的clone()方法与DOM中的cloneNode()方法
JQ中的clone()方法与DOM中的cloneNode()方法 cloneNode()定义和用法 cloneNode()方法创建节点的拷贝,并返回该副本. 语法: node.cloneNode(de ...
- 1、File类的API
通过Api我们可知,File类是java一个内置类,被封装到java.io.jar包中 其构造方法有一下3种 其方法常用的有以下几种
- spring注解支持
Spring基于注解实现Bean定义支持如下三种注解: Spring自带的@Component注解及扩展@Repository.@Service.@Controller JSR-250 1.1版本中中 ...
- 使用 Spark MLlib 做 K-means 聚类分析[转]
原文地址:https://www.ibm.com/developerworks/cn/opensource/os-cn-spark-practice4/ 引言 提起机器学习 (Machine Lear ...