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 ...
随机推荐
- memset 初始化数组
memset是初始化一段内存区域的函数,其头文件是<string.h>,以前经常使用出现错误,整理一下. C++ Reference对于memset的定义为: void * memset ...
- 学会用Clang来进行内存泄露分析
最近项目出现了内存泄露的问题,对于PC x86平台来说,一点点的内存泄露往往不会出错,很难进行debug调试.这个时候我们可以用到苹果给我们带来的神器--Clang编译器来进行内存泄露分析检测,开关打 ...
- sublime常用插件及配置,自留自用
1.Angularjs 写angularjs经常操作template文件,没有一个ng-xx的提示真的很蛋疼是不是,有些服务的名字记不住是不是,那就用这个插件吧 2.AutoFileName 如果你的 ...
- C# ,asp.net 获取当前,相对,绝对路径(转)
C# ,asp.net 获取当前,相对,绝对路径 一.C#获取当前路径的方法: . System.Diagnostics.Process.GetCurrentProcess().MainModule. ...
- C# 构造函数如何调用父类构造函数或其他构造函数
class C : B{ C() : base(5) // call base constructor B(5) { } C(int i) : this() // ca ...
- window.showModalDialog以及window.open用法简介
.可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象.例如:------------------------------parent.htm<script& ...
- PHP转换IP地址到真实地址的方法详解
本篇文章是对PHP转换IP地址到真实地址的方法进行了详细的分析介绍,需要的朋友参考下 想要把IPv4地址转为真实的地址,肯定要参考IP数据库,商业的IP数据库存储在关系型数据库中,查询和使用都非常 ...
- 关于cmd的东西
为了督促自己,就从简单的开始学起,希望用博客来纪念我的成长 1)输入 CMD 回车.进入DOS提示符状态下.输入cd\ 回车 表示进入 c:\> 也到了C盘根目录下 2)d: 回车 是进入D盘当 ...
- HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
Problem Description As is known to all, Sempr(Liangjing Wang) had solved more than 1400 problems on ...
- RMQ算法讲解
RMQ算法 引入: 例1.题目描述 输入N个数和M次询问,每次询问一个区间[L,R],求第L个数到R个数之间的最大值. 第一种方法:大暴力之术. 但是……时间复杂度最坏会达到 $O(NM)$,一半 ...