WOJ 1538 B - Stones II

本文介绍了一道名为Problem1538-B石头II的问题,该问题涉及动态规划算法。任务是在给定的限制条件下,通过选择特定数量的宝石来最大化总价值。文章提供了完整的代码实现,采用动态规划的方法,定义dp[i][j]表示考虑前i个宝石还需要选j个的最佳方案。

Problem 1538 - B - Stones II
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 416 Accepted: 63 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 n 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

解题:动态规划 dp[i][j]表示考察了前面i个 还要选j个

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 1010;
 5 struct stone{
 6     int a,b;
 7     bool operator<(const stone &rhs)const{
 8         return b < rhs.b;
 9     }
10 }s[maxn];
11 int dp[maxn][maxn];
12 int main(){
13     int n;
14     while(scanf("%d",&n),n){
15         memset(dp,-INF,sizeof dp);
16         dp[0][0] = 0;
17         for(int i = 1; i <= n; ++i){
18             dp[0][i] = 0;
19             scanf("%d%d",&s[i].a,&s[i].b);
20         }
21         sort(s+1,s+n+1);
22         for(int i = 1; i <= n; ++i)
23             for(int j = 0; j <= n; ++j)
24                 dp[i][j] = max(dp[i-1][j],dp[i-1][j+1] + s[i].a - s[i].b*j);
25         printf("%d\n",dp[n][0]);
26     }
27     return 0;
28 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4789481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值