Problem 1538 - B - Stones II
Time Limit: 1000MS   Memory Limit: 65536KB  
Total Submit: 428  Accepted: 64  Special Judge: No
Description

Xiaoming took the flight MH370 on March 8, 2014 to China to take the ACM contest in WHU. Unfortunately, when the airplane crossing the ocean, a beam of mystical light suddenly lit up the sky and all the passengers with the airplane were transferred to another desert planet.

When waking up, Xiaoming found himself lying on a planet with many precious stones. He found that:

There are precious stones lying on the planet, each of them has 2 positive values ai and bi. Each time Xiaoming can take the ith of the stones ,after that, all of the stones’ aj (NOT including the stones Xiaoming has taken) will cut down bi units.

Xiaoming could choose arbitrary number (zero is permitted) of the stones in any order. Thus, he wanted to maximize the sum of all the stones he has been chosen. Please help him.

Input
The input consists of one or more test cases.

First line of each test case consists of one integer n with 1 <= n <= 1000.
Then each of the following n lines contains two values ai and bi.( 1<= ai<=1000, 1<= bi<=1000)
Input is terminated by a value of zero (0) for n. 

Output
For each test case, output the maximum of the sum in one line.
Sample Input
1
100 100
3
2 1
3 1
4 1
0
Sample Output
100
6
Hint

Source

题意 :给你n堆石子,当取走其中的一个后会使剩余所有的石子的费用全部减少当前石子的 b 值,可以选取任意数量的石子,问最终能够取得的最大收益是多少。

思路分析: 这个问题倒着去想比较方便,当取倒数第一个石子时,此时他不会影响任何石子,当取倒数第二个石子时,此时他所能影响的只有倒数第一个,想到这个,dp的转移方程就很明白了, dp[i][j] 表示当枚举到第 i 个石子的时候,此时这个石子作为倒数第 j 个石子的最大收益,则由转移方程

d[i][j]=max(d[i-1][j],d[i-1][j-1]+ai-(j-1)*bi)

在选取的时候,因为当前的情况是会影响后面的,若让这个过程最优,则需要你去贪心一下,对全部的石子排一个序,b值较小的往前面放,b值相同时,a值较大的往前面放。

代码示例:(未测试)

struct node
{
int a, b; bool operator< (const node &v){
if (b == v.b) return a < v.a;
return b > v.b;
}
}pre[1005];
int dp[1005][1005]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n; while(~scanf("%d", &n) && n){
for(int i = 1; i <= n; i++){
scanf("%d%d", &pre[i].a, &pre[i].b);
}
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]+pre[i].a-(j-1)*pre[i].b);
}
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[n][i]); printf("%d\n", ans);
}
return 0;
}

woj - 将一个问题转换为背包问题的更多相关文章

  1. int([x[, base]]) : 将一个字符转换为int类型,base表示进制

    int([x[, base]]) : 将一个字符转换为int类型,base表示进制 >>> int(-12) -12 >>> int(-12.00) -12 > ...

  2. 如何快速的将一个str转换为list

    # -*- coding: cp936 -*- #python 27 #xiaodeng #如何快速的将一个str转换为list str='python' print list(str)#['p', ...

  3. long([x[, base]]) :将一个字符转换为long类型

    python的int型最大值和系统有关,32位和64位系统看到的结果是不一样,分别为2的31次方减1和2的63次方减1,可以通过sys.maxint查看此值. >>> import ...

  4. 将一个mapList转换为beanList

    public static <T> List<T> copyMapToBean(   List<Map<String, Object>> resultM ...

  5. 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".

    题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...

  6. 在.NET中使用管道将输出流转换为输入流

    最近在写一段代码,将本地文件压缩加密后发送到服务器,发送到服务器的类用一个输入流作为参数获取要上传的数据,而压缩类和加密类都是输出流. 如何将输出流转换为输入流,最直观的方法是缓存输出流的全部内容到内 ...

  7. 对象列表转换为DataTable或DataTable转换为对象列表.

    /**********************************************************************************/ // 说明: 数据转换工具. ...

  8. iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输

    http://blog.csdn.net/worldzhy/article/details/49982491 将NSArray.NSDictionary转换为JSON格式进行网络传输,是经常用到的,但 ...

  9. js字符串转换为数字 总结

    a. 将一个字符串转换为数字的一种缺少些技巧但是很清楚明白的方法就是:把Number()构造函数作为一个函数来调用:  var number = Number(string_value); b. pa ...

随机推荐

  1. Vue导航栏在特定的页面不显示~

    最近写vue项目遇到一些问题,我把导航栏组件放在了app.vue中,让他在每个页面都能显示了,但遇到了一个问题,在登录以及注册页面导航栏是不合理不允许存在的 解决方法: 公共模块的内容可以放在App. ...

  2. Python--day60--一个简单(不完整)的web框架

  3. 访问HTML可以,访问PHPfile not found

    www目录的所有者和所属组都改为nginx用户试一下. 参考命令: chown nginx.nginx /home/www ps aux |grep nginx   看一下您的nginx是以哪个用户的 ...

  4. webpack优化 -- compression-webpack-plugin 开启gzip

    webpack优化 -- compression-webpack-plugin 开启gzip 打包的时候开启gzip可以大大减少体积,非常适合于上线部署.下面以vue-cli2.x项目为例,介绍如何在 ...

  5. H3C IPv6地址表示方式

  6. 【30.43%】【codeforces 746C】Tram

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Canvas文本设置

    本文的应用对Canvas文本设置相关属性进行了汇总,具体使用说明请参考下面代码: <!DOCTYPE html> <html lang="zh"> < ...

  8. 面试中常考的字符串操作方法大全,包含ES6

    原文链接:http://caibaojian.com/js-string.html 一.charAt() 返回在指定位置的字符. var str="abc" console.log ...

  9. jQuery 工具类函数-检测对象是否为原始对象

    调用名为$.isPlainObject的工具函数,能检测对象是否为通过{}或new Object()关键字创建的原始对象,如果是,返回true,否则,返回false值,调用格式为: $.isPlain ...

  10. 闲着没事,做个chrome浏览器插件,适合初学者

    时光偷走的,永远都是我们眼皮底下看不见的珍贵. 本插件功能:替换掉网页中的指定图片的src地址. 使用插件前: 使用插件后: 鲜花(闲话):这个网站的不加水印的图片连接被保存在,图片的data-ima ...