zoukankan      html  css  js  c++  java
  • [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)

    [USACO09OPEN] 工作调度Work Scheduling

    题意翻译

    约翰有太多的工作要做。为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有10^9个单位时间。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 10^6)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 10^9),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=10^9 ). 在给定的工作利润和截止时间下,约翰能够获得的利润最大为多少.

    题目描述

    Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.

    His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs

    conveniently numbered 1..N for work to do. It is possible but

    extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.

    Job i has deadline D_i (1 <= D_i <= 1,000,000,000). If he finishes job i by then, he makes a profit of P_i (1 <= P_i <= 1,000,000,000).

    What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.

    输入输出格式

    输入格式:

    • Line 1: A single integer: N

    • Lines 2..N+1: Line i+1 contains two space-separated integers: D_i and P_i

    输出格式:

    • Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn.

    输入输出样例

    输入样例#1:

    3
    2 10
    1 5
    1 7

    输出样例#1:

    17

    说明

    Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).

    Solution

    很容易想到按时间从小到大排序,因为它的截止时间越长,我们就可以了把前面的时间去留给其他物品,然后贪心的去选,但是这样会WA,为什么?

    看一下这种情况,我们在前面选了价值较小的工作,但是当前有一个价值比它大得多的工作我们却没法选,这个时候就会后悔,所以这是一道可以反悔的贪心题

    那么怎么反悔呢?我们用一个小根堆去维护,每次选的工作,我们把它的价值丢进小根堆,如果碰到一个选不了的工作,我们就拿它和堆顶比较,如果比它大,那么堆顶其实没必要选的,所以把它弹出去

    具体情况是什么样子的呢?(以下我们用(t)表示工作的截止时间)

    因为一个单位时间只能做一个工作,所以堆的元素个数表示至少已经过了(size)个单位时间

    1. 当前工作的t小于堆的元素个数,说明当前工作已经截止了,不用管
    2. 当前工作的t大于堆内元素个数,直接插入
    3. 当前工作的t等于堆内元素个数,与堆顶比较,看谁更优
    

    Code

    #include<bits/stdc++.h>
    #define in(i) (i=read())
    #define il extern inline
    #define rg register
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define lol long long
    using namespace std;
    
    const lol N=1e6+10;
    
    lol read() {
        lol ans=0, f=1; char i=getchar();
        while (i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
        while (i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+(i^48), i=getchar();
        return ans*f;
    }
    
    struct Node {
        lol day,v;
        bool operator < (const Node &a) const {return day<a.day;}
    }t[N];
    
    priority_queue<lol,vector<lol>,greater<lol> >q;
    
    int main()
    {
        lol n,ans=0; in(n);
        for (lol i=1;i<=n;i++)
            in(t[i].day), in(t[i].v);
        sort(t+1,t+1+n);
        for (lol i=1;i<=n;i++) {
            if(t[i].day<q.size()) continue;
            else if(t[i].day==q.size()) {
                if(t[i].v>q.top()) {
                    ans-=q.top(); q.pop();
                    q.push(t[i].v); ans+=t[i].v;
                }
            }else ans+=t[i].v,q.push(t[i].v);
        }cout<<ans<<endl;
    }
    
  • 相关阅读:
    pthread_once函数的简单示例
    pthread_join直接决定资源是否能够及时释放
    非分离线程未使用join函数例子:
    一个HTTP打趴80%面试者
    BM和KMP字符串匹配算法学习
    STL 几个容器的底层实现
    指针的引用(*&)与指针的指针(**)
    Maven 环境变量设置
    配置JAVA的环境变量
    Maven报错 解决方案。ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id
  • 原文地址:https://www.cnblogs.com/real-l/p/9911297.html
Copyright © 2011-2022 走看看