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 ",dp[n][0]); 26 } 27 return 0; 28 }