码迷,mamicode.com
首页 > 其他好文 > 详细

supermarket [POJ1456]

时间:2020-02-18 14:47:01      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:i++   商品   perm   algo   typedef   ios   names   cout   int   

** 算法二:
贪心策略

  1. 优先考虑利润大的商品
  2. 每个商品,售出的时间越晚越好。
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10000 + 10;
int n;
typedef pair<int, int> PII;
PII a[N];

int fa[N];
int get(int x)
{
    if (fa[x] == x)
        return x;
    return fa[x] = get(fa[x]);
}
int main()
{
    while (cin >> n)
    {
        int m = 0; int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i].first >> a[i].second;
            m = max(m, a[i].second);
        }
        sort(a + 1, a + n + 1);
        reverse(a + 1, a + n + 1);

        for (int i = 1; i <= m; i++)
        {
            fa[i] = i;
        }
        
        for (int i = 1; i <= n; i++)
        {
            int day = get(a[i].second);
            if (!day)
                continue;
            ans += a[i].first;
            fa[day] = get(day - 1);
        }
        cout << ans << endl;
    }
}

supermarket [POJ1456]

标签:i++   商品   perm   algo   typedef   ios   names   cout   int   

原文地址:https://www.cnblogs.com/keik/p/12326037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!