zoukankan      html  css  js  c++  java
  • Atcoder ABC137D:Summer Vacation(贪心)

    D - Summer Vacation


    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 400 points

    Problem Statement

    There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of Bi after Ai days from the day you do it.

    You can take and complete at most one of these jobs in a day.

    However, you cannot retake a job that you have already done.

    Find the maximum total reward that you can earn no later than M days from today.

    You can already start working today.

    Constraints

    • All values in input are integers.
    • 1N105
    • 1M105
    • 1Ai105
    • 1Bi104

    Input

    Input is given from Standard Input in the following format:

    N  M
    A1 B1
    A2 B2
    
    AN BN
    

    Output

    Print the maximum total reward that you can earn no later than M days from today.

    Sample Input 1

    3 4
    4 3
    4 1
    2 2
    

    Sample Output 1

    5
    

    You can earn the total reward of 5 by taking the jobs as follows:

    • Take and complete the first job today. You will earn the reward of 3 after four days from today.
    • Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

    题意

    一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在第Ai天拿到Bi的工资,问M天内最多可以拿到多少工资

    思路

    贪心,按照领取工资间隔升序排序

    设置一个优先队列用来储存当前可以做的任务并且可以在M天内拿到工资的任务的钱数,每次取可以拿到的最大值

    代码

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 #define ull unsigned long long
     4 #define ms(a,b) memset(a,b,sizeof(a))
     5 const int inf=0x3f3f3f3f;
     6 const ll INF=0x3f3f3f3f3f3f3f3f;
     7 const int maxn=1e6+10;
     8 const int mod=1e9+7;
     9 const int maxm=1e3+10;
    10 using namespace std;
    11 struct wzy
    12 {
    13     int day,money;
    14 }p[maxn];
    15 bool cmp(wzy u,wzy v)
    16 {
    17     return u.day<v.day;
    18 }
    19 int main(int argc, char const *argv[])
    20 {
    21     ios::sync_with_stdio(false);
    22     cin.tie(0);
    23     int n,m;
    24     cin>>n>>m;
    25     for(int i=0;i<n;i++)
    26         cin>>p[i].day>>p[i].money;
    27     sort(p,p+n,cmp);
    28     priority_queue<int>que;
    29     int ans=0;
    30     int pos=0;
    31     for(int i=1;i<=m;i++)
    32     {
    33         while(p[pos].day<=i&&pos<n)
    34             que.push(p[pos++].money);
    35         if(!que.empty())
    36             ans+=que.top(),que.pop();
    37     }
    38     cout<<ans<<endl;
    39     return 0;
    40 }
  • 相关阅读:
    打成jar包运行,依然可以找到指定路径的xml
    Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细
    Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader
    Springboot + Mybatis + Ehcache
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class
    缓存的设计与使用
    一二级缓存
    HttpServletResponse 返回的json数据不是json字符串,而是json对象
    消息中间件选型
    为什么要使用MQ消息中间件?
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11340019.html
Copyright © 2011-2022 走看看