首页 技术 正文
技术 2022年11月15日
0 收藏 751 点赞 4,505 浏览 2057 个字

Problem 1538 – B – Stones IITime Limit: 1000MS   Memory Limit: 65536KB  
Total Submit: 428  Accepted: 64  Special Judge: NoDescription

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.

InputThe 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. OutputFor each test case, output the maximum of the sum in one line.Sample Input1
100 100
3
2 1
3 1
4 1
0Sample Output100
6Hint

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902