您的位置:首页 > 理论基础 > 计算机网络

第三届华中区程序设计邀请赛暨武汉大学第十二届校赛 网络预选赛 水题解题报告

2014-03-31 09:08 357 查看
Problem 1537 - A - Stones I

Time Limit: 1000MS  Memory Limit: 65536KB   

Total Submit: 470  Accepted: 72  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 (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
0

3
Hint

Source

简单贪心策略,枚举取石头的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1010;

int a
, b
, c
;

bool cmp(int x, int y){
return x > y;
}

int main(){
int n;
while(scanf("%d", &n) != EOF && n){
for(int i = 0; i < n; i++)
scanf("%d%d", &a[i], &b[i]);
int ans = -0x3fffffff;
for(int k = 1; k <= n; k++){
for(int i = 0; i < n; i++)
c[i] = a[i] - k * b[i];
sort(c, c + n, cmp);
int sum = 0;
for(int i = 0; i < k; i++){
sum += c[i];
}
//  cout << k << " " << ans << endl;
ans = max(ans, sum);
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐