HDU -2670 Girl Love Value
这道题是刚好装满的背包问题,刚好选取k个,状态转移方程为dp[i][j] = max( dp[i - 1][j], dp[i - 1][j - 1] + Li - Bi(j - 1) )
dp[i][j] 表示从前 i 个男孩中选取 j 个的 Li 的最大值, 首先按照Bi 排一下序,这个是利用贪心的思想,因为那样才能获得最优解,按照递减排序,这样才能找到最大值,然后就是dp了,状态转移方程的意思就是第 j 个去或者不 取,代码如下
代码一(二维数组版):
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Happy{
int Li, Bi;
};
const int N = ;
int dp[N][N];
Happy happy[N];
bool cmp(Happy a, Happy b)//递减排序
{
return a.Bi > b.Bi;
}
int main()
{
int n, v;
while (~scanf("%d %d", &n, &v))
{
memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
scanf("%d", &happy[i].Li);
for (int i = ; i <= n; i++)
scanf("%d", &happy[i].Bi);
sort(happy + , happy + n + , cmp);//贪心思想
for (int i = ; i <= n; i++)
{
for (int j = ; j <= i && j <= v; j++)
dp[i][j] = max(dp[i - ][j], dp[i - ][j - ] + happy[i].Li - happy[i].Bi * (j - ));
}
printf("%d\n", dp[n][v]);
} return ;
}
代码二(优化空间版):
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Happy{
int Li, Bi;
};
const int N = ;
int dp[N];
Happy happy[N];
bool cmp(Happy a, Happy b)//递减排序
{
return a.Bi > b.Bi;
}
int main()
{
int n, v;
while (~scanf("%d %d", &n, &v))
{
memset(dp, , sizeof(dp));
for (int i = ; i <= n; i++)
scanf("%d", &happy[i].Li);
for (int i = ; i <= n; i++)
scanf("%d", &happy[i].Bi);
sort(happy + , happy + n + , cmp);//贪心思想
for (int i = ; i <= n; i++)
{
for (int j = v; j >= ; j--)
/*这句话等价于dp[j] = max(dp[j], dp[j - 1] + happy[i].Li - happy[i].Bi * (j - 1)),只不过用if更快*/
if (dp[j - ] + happy[i].Li - happy[i].Bi * (j - ) > dp[j])
dp[j] = dp[j - ] + happy[i].Li - happy[i].Bi * (j - );
}
printf("%d\n", dp[v]);
} return ;
}
HDU -2670 Girl Love Value的更多相关文章
- hdu 2669 Romantic (乘法逆元)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDU 2669 Romantic(裸的拓展欧几里得)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- ERROR:the server has either erred or is incapable of performing the requested operation
openstack中,有时会经常出现这种错误,原因无二,一是安全组没有设置正确,二是openstack中网络配置会有些问题或者是相关的服务没有启动. 解决方法:1.安全组问题在nova.conf和ne ...
- Tomcat安装阿里云免费证书
安装证书 Tomcat支持JKS格式证书,从Tomcat7开始也支持PFX格式证书,两种证书格式任选其一.下载包中包含PFX格式证书和密码文件. 1.PFX证书安装 找到安装 Tomcat 目录下该文 ...
- webBrowser(网络转载)
C#WebBrowser控件使用教程与技巧收集--苏飞收集 先来看看常用的方法 [C#] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 ...
- 解决css3遮罩层挡住下面元素事件的方法
比如大家常看到的鼠标移入图片中,会有一个挡住图片的黑色半透明遮罩层,上面还有文字介绍,这时候就会遇到该层遮挡住下面图片的跳转链接事件,这时候怎么办呢?有个简单的css3属性可以快速解决该问题:poin ...
- python之7-2类的继承与多态
类的继承的意思就如同父子关系一样,这个儿子继承了父亲的一切,但是在某些地方(属性)相同的时候,儿子的属性大于老子的属性(覆盖),最底层类,总会继承最接近它的那个类的属性init 类的多态总是和继承相连 ...
- JS小技巧大本事(持续更新)
1. 复制N个字符 String.prototype.repeat = function(num){ return (new Array(++num)).join(this); } var a = ' ...
- CSS实现table td中文字的省略与显示
所谓省略就是把多余的字以“...”显示出来,而显示则是当鼠标移动到td上时,把省略的字重新显示出来.对于一个table,兼容IE与FF.Chrome的省略方式CSS写法: table{ width:2 ...
- h.264参考图像列表、解码图像缓存
1.参考图像列表(reference picture list) 一般来说,h.264会把需要编码的图像分为三种类型:I.P.B,其中的B.P类型的图像由于采用了帧间编码的这种编码方式,而帧间编码又是 ...
- 十大众筹PC:硅谷新生代如何打造下一代计算机
十大众筹PC:硅谷新生代如何打造下一代计算机 来源:CNET科技资讯网 众筹革命已经让众多吸引人的台式机,笔电和平板PC诞生.下面就是最引人注意和最成功的典范. 尽管PC市场不再象过去那样是一 ...
- BZOJ2718: [Violet 4]毕业旅行
2718: [Violet 4]毕业旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 229 Solved: 126[Submit][Status ...