zoukankan      html  css  js  c++  java
  • 【BZOJ 1572】 工作安排

    【题目链接】

                https://www.lydsy.com/JudgeOnline/problem.php?id=1572

    【算法】

              贪心

              先将这些工作按截至时间排序

              建立一个小根堆,当决策是否完成一项工作时,若堆的大小小于该工作的截止时间,则将这项工作所获得的利润放入堆,否则,将这项工作的利润与堆顶比较,如果比堆顶大,则将堆顶弹出,将这项工作的利润放入堆中

    【代码】

                

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 100010
    
    struct info
    {
            int d,p;
    } a[MAXN];
    
    int i,n;
    long long ans;
    priority_queue< int,vector<int>,greater<int> > q;
    
    bool cmp(info a,info b) 
    {
            return a.d < b.d;
    }
    
    int main() 
    {
            
            scanf("%d",&n);
            for (i = 1; i <= n; i++) scanf("%d%d",&a[i].d,&a[i].p);
            sort(a+1,a+n+1,cmp);
            for (i = 1; i <= n; i++)
            {
                    if (q.size() < a[i].d) q.push(a[i].p);
                    else if (a[i].p > q.top()) 
                    {
                            q.pop();
                            q.push(a[i].p);
                    }
            }
            while (!q.empty())
            {
                    ans += (long long)(q.top());
                    q.pop();
            }
            printf("%lld
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    while循环学习之统计流量
    MySQL的启动脚本
    UVA 725 Division
    UVA 712 S-tree
    UVA 514
    字典树
    UVA 1595 multimap 的应用
    C++ map 和 multimap
    浮点数
    UVA 227
  • 原文地址:https://www.cnblogs.com/evenbao/p/9250514.html
Copyright © 2011-2022 走看看