HDU3732 背包DP
Ahui Writes Word
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2853 Accepted Submission(s): 1018
all know that English is very important, so Ahui strive for this in
order to learn more English words. To know that word has its value and
complexity of writing (the length of each word does not exceed 10 by
only lowercase letters), Ahui wrote the complexity of the total is less
than or equal to C.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
first line of each test case are two integer N , C, representing the
number of Ahui’s words and the total complexity of written words. (1 ≤ N
≤ 100000, 1 ≤ C ≤ 10000)
Each of the next N line are a string and
two integer, representing the word, the value(Vi ) and the complexity(Ci
). (0 ≤ Vi , Ci ≤ 10)
Input data is huge,please use “scanf(“%s”,s)”
//很明显是01背包问题,但本题数据太大普通的01背包会超时,由于v和c都不大于10,最多有11*11组数据,统计每组数据有多少个,所以本题可以看做是多重背包
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[];
int N,C;
void zeroonepack(int v,int m,int ttl)
{
for(int i=ttl;i>=v;i--)
f[i]=max(f[i],f[i-v]+m);
}
void complitpack(int v,int m,int ttl)
{
for(int i=v;i<=ttl;i++)
f[i]=max(f[i],f[i-v]+m);
}
void multipack(int v,int m,int c,int ttl)
{
if(c*v>=ttl)
complitpack(v,m,ttl);
else
{
int k=;
while(k<c)
{
zeroonepack(k*v,k*m,ttl);
c-=k;
k*=;
}
zeroonepack(c*v,c*m,ttl);
}
}
int main()
{
char s[];
int val,cont;
int eg[][];
while(scanf("%d%d",&N,&C)!=EOF){
memset(f,,sizeof(f));
memset(eg,,sizeof(eg));
for(int i=;i<=N;i++)
{
scanf("%s%d%d",s,&val,&cont);
eg[val][cont]++;
}
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(eg[i][j]==) continue;
multipack(j,i,eg[i][j],C);
}
}
printf("%d\n",f[C]);
}
return ;
}
HDU3732 背包DP的更多相关文章
- 背包dp整理
01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
- 树形DP和状压DP和背包DP
树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...
随机推荐
- SurfaceView
我们先来看下官方API对SurfaceView的介绍 SurfaceView的API介绍 Provides a dedicated drawing surface embedded inside of ...
- 如何在MFC中添加对话栏
- Blog Starting...
30出头,开始Blog记录学习生活的点滴,待40时再回来一看.
- JMeter常用字符串相关函数
JMeter的惯用函数使用-字符串相关 主要的函数如下:1.将字符串转为大写或小写: ${__lowercase(Hello,)} ${__uppercase(Hello,)}2.生成字符串: _ ...
- js总结-面向对象编程,DOM,BOM
- Hibernate,一对一外键单向 记录。Timestamp 的一个坑。
首先是2张表 表A: 表B: 其中表B中的FormBaseId对应表A中的SubjectID. 数据库中没有设置外键关系. 下面是2个对应的实体 package questionnaire.model ...
- 我的c++学习(5)switch语句详解
#include "stdafx.h" #include<iostream> using namespace std; #include<iomanip> ...
- [bzoj3813]奇数园
仿佛现在已经完成了做题之前先开个坑的习惯,也许是为了逼迫自己去刷一些神题吧...然并卵,该剩的好多坑还是剩着呢. [bzoj3813]一道线段树好题.已经把数论忘光光了. 欧几里德算法 扩展欧几里德算 ...
- Dijkstra+计算几何 POJ 2502 Subway
题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...
- xampp的Apache无法启动解决方法
XAMPP Apache 无法启动原因1(缺少VC运行库): 这个就是我遇到的问题原因,下载安装的XAMPP版本是xampp-win32-1.7.7-VC9,而现有的Windows XP系统又没有安装 ...