WOJ 武大校赛 Problem 1537 - A - Stones I

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


告诉你n个物品, n<=1000, 每个物品有两个值a和b, 每选择一个物品, 所有物品包括这件选中的物品的a 就要减去这件选中物品的 b值,问你选中物品的a的总和Max最大为是多少。


主要是看题面的条件,  既然n<=1000就可能存在O(n^2)复杂度的解。

由于物品数不会超过1000, 就可以枚举放入的物品总数x, 用n^2的复杂度解决问题。

首先从1到n枚举放入的物品总数为x,那么假如放了第k件物品,就可以使得总收益增加a[k]-b[k]*x,所以就对所有n件物品按a[k]-b[k]*x从大到小排一个序,再将前x件物品计算好,更新一下Max。最后把Max输出来。


直接贴代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

int a[1005], b[1005], c[1005];

bool cmp(const int i, const int j) {
    return i > j;
}

int main() {
    int n, k, max, sum;
    while (~scanf("%d", &n) && n) {
        max = 0;
        for (int i = 0; i < n; i++)
            scanf("%d%d", &a[i], &b[i]);
        for (k = 0; k <= n; k++) {
            sum = 0;
            for (int i = 0; i < n; i++)
                c[i] = a[i] - k * b[i];
            sort(c, c + n, cmp);
            for (int i = 0; i < k; i++)
                sum += c[i];
            if (max < sum)
                max = sum;
        }
        printf("%d\n", max);
    }
    return 0;
}





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值