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 ...
随机推荐
- protocol buffer VS 2013编译出错
protocol buffer 在VS2013编译会出现以下错误. 解决办法 把宏加上, 问题解决. 注: 该错误只出现在Debug版本.
- linux进程创建
1. 进程是程序的执行,会被加载到内存中,每个进程包括程序的代码和数据,其中数据包括程序的变量的数据,外部数据,程序堆栈. 2. Linux中,输入命令,如vi main.c 通过shell来执行, ...
- OpenGL画图旋转
#include<gl/glut.h>#include<gl/GL.h>#include<gl/GLU.h>#include<math.h>#inclu ...
- js关于DOM和BOM
关于BOM和DOM BOM 下面一幅图很好的说明了BOM和DOM的关系 BOM提供了一些访问窗口对象的一些方法,我们可以用它来移动窗口位置,改变窗口大小,打开新窗口和关闭窗口,弹出对话框,进行导航以及 ...
- DeDe内容调用img
文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大小, {dede:field.image/} <img src="{dede:field.litpic ...
- jquery easy ui 学习 (5) windowlayout
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- error: File not found by glob???
引发上述问题的几种情形: 1.rpm包的包名中含有“[.]”这类特殊符号; 2.执行rpm -ivh xxxx.rpm时,xxxx包名不存在; 3.rpm包顶层的目录名不存在或者存在问题: - ...
- Yii2——MYSQL操作
先创建连接对象 $connection = new \yii\db\Connection([ 'dsn' => $dsn, 'username' => $username, 'passwo ...
- java开发规范
hbh 开发规范文档 一:目的 使本组织能以标准的,规范的方式设计和编码.通过建立编码规范,以使每个开发人员 养成良好的编码风格和习惯:并以此形成开发小组编码约定,提高程序的可靠性,可读性, 可修改性 ...
- 点击Textbox 给日历控件赋值
1.前台代码: <asp:TextBox ID="TextBox1" runat="server" onfocus="return show() ...