zoukankan      html  css  js  c++  java
  • 水题:51Nod 1163-最高的奖励

    最高的奖励

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题

    Description

    有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。

    Input

    第1行:一个数N,表示任务的数量(2 <= N <= 50000)
    第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间E[i]以及对应的奖励W[i]。(1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)

    Output

    输出能够获得的最高奖励。

    Input

    7
    4 20
    2 60
    4 70
    3 40
    1 30
    4 50
    6 10

    Output

    230


    解题心得:

    1. 开始还以为是一个动态规划,后来动态规划超时了,再看发现被欺骗了,一个优先队列就过了,哎,因为他每次玩成一个任务的时间都只能是一天啊,优先队列的size就可以代表天数了啊,哎,一个贪心的思想。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 5e4+100;
    struct node
    {
        long long va;
        friend bool operator < (node a,node b)
        {
            return a.va > b.va;
        }
    };
    
    struct N
    {
        long long va,time;
    } res[maxn];
    
    bool cmp(N a,N b)
    {
        return a.time < b.time;
    }
    
    priority_queue <node> qu;
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            for(int i=0; i<n; i++)
                scanf("%lld%lld",&res[i].time,&res[i].va);
    
            node k;
            sort(res,res+n,cmp);
            for(int i=0; i<n; i++)
            {
                if(res[i].time == qu.size() && res[i].va > qu.top().va)//时间刚好是截止日期但是第一个花费一天得到的价值比当前更小
                {
                    qu.pop();
                    k.va = res[i].va;
                    qu.push(k);
                }
                else if(res[i].time > qu.size())//当前时间还没达到这个事件的截止日期
                {
                    k.va = res[i].va;
                    qu.push(k);
                }
            }
            long long sum = 0;
            while(!qu.empty())
            {
                sum += qu.top().va;
                qu.pop();
            }
            printf("%lld
    ",sum);
        }
    }
    
  • 相关阅读:
    Python使用SMTP模块、email模块发送邮件
    harbor搭建及使用
    ELK搭建-windows
    ELK技术栈之-Logstash详解
    【leetcode】1078. Occurrences After Bigram
    【leetcode】1073. Adding Two Negabinary Numbers
    【leetcode】1071. Greatest Common Divisor of Strings
    【leetcode】449. Serialize and Deserialize BST
    【leetcode】1039. Minimum Score Triangulation of Polygon
    【leetcode】486. Predict the Winner
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107300.html
Copyright © 2011-2022 走看看