这道题是刚好装满的背包问题,刚好选取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的更多相关文章

  1. hdu 2669 Romantic (乘法逆元)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  3. HDU 2669 Romantic(裸的拓展欧几里得)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. 【USACO 2.1.5】海明码

    [题目描述] 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个 ...

  2. 安装 php

    1.yum安装php yum install php 2.配置 apache 支持 php a.找到httpd.conf find / -name  httpd.conf b.编辑 httpd.con ...

  3. 关闭iOS的自动更新

    Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了

  4. EclipsePHP Studio 常用设置笔记

    工作需要,学习PHP使用EclipsePHP Studio开发工具, 习惯整理下常用的使用设置,分享一下吧: 1.窗口-首选项-常规-工作空间,把文本文件编码改为utf8,以后再新建文件就默认是utf ...

  5. CodeForces 569A 第六周比赛C踢

    C - C Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. SCJP_104——题目分析(5)

    18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Int ...

  7. JS之路——日期函数

    时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...

  8. LRU 算法简单实现

    在学习很多服务器软件中,当内存不够,而需要淘汰内存的时候,一般会使用LRU算法,便产生了浓厚的兴趣.在学习操作系统的过程中发现LRU在系统中用寄存器和栈来实现.所以我就尝试着学习用栈来解决LRU的问题 ...

  9. 转:So Easy!让开发人员更轻松的工具和资源

    Cascade Framework 很独特的 CSS 框架,进行了模块化划分,分类排版.表格.颜色.图标和打印样式等等. Mueller Grid System 一个模块化的网格系统,用于响应式或者固 ...

  10. 统计指定时间段的访问真正WEB页面(去除静态请求)的IP的TOP100排行

    最近就在磨这个脚本以达到SEO同事要求哈. awk -v b=[21/Apr/2015:15:46 -v a=[21/Apr/2015:16:46 '$4 > b && $4 & ...