标签:style http color os io for ar 时间
题意:有N个物品,每个物品在都有一个截止日期,如果在截止日期之前(包括截止日期)卖出将会获得相应的利润,卖出物品需要一个单位时间,问最多能获得多少利润?
思路:将利润从大到小排序,尽量在该物单位时间出售利润大的物品,这样就能使得从利润达到最大。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
struct product{
    int money, t;
}p[MAXN];
int n, vis[MAXN];
int cmp(product a, product b) {
    return a.money > b.money;
}
int main() {
    while (scanf("%d", &n) == 1) {
        for (int i = 0; i < n; i++)  
            scanf("%d%d", &p[i].money, &p[i].t); 
        sort(p, p + n, cmp); 
        memset(vis, 0, sizeof(vis));
        long long ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = p[i].t; j >= 1; j--) {
                if (vis[j] == 0) {
                    ans += p[i].money;
                    vis[j] = 1; 
                    break;
                }
            }  
        }
        cout << ans << endl;
    }
    return 0;
}UVA1316- Supermarket,布布扣,bubuko.com
标签:style http color os io for ar 时间
原文地址:http://blog.csdn.net/u011345461/article/details/38512771