https://www.luogu.org/problem/P2949

题目描述

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 Di (1 <= Di <= 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 <= Pi <= 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: Di and Pi

输出描述:

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

输入


输出


说明

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

每件物品代价相同,但价值不同。那么我们很容易想到,在满足限制的情况下,我们肯定会选择价值尽可能大的物品。

我们可否用背包来实现呢,答案是否定的,或者说我不会QwQ

那么,我们来看看贪心

首先思路就是先按时间排序(不是那个1e8啊,是截止日期),然后如果一个工作有时间去做,就先做了它(听起来有点怪),然后把它的价值压入一个小根堆。

当我们找到一个没法做却价值比当前堆顶高的工作时,我们就放弃那个最小的工作,用做它的时间去做这个价值更高的工作。

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=1e5+;
using namespace std; struct node
{
int t;
int m;
}a[]; LL ans;
priority_queue<int ,vector<int>,greater<int> >q;
bool cmp(node a,node b)
{
return a.t<b.t;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d %d",&a[i].t,&a[i].m);
}
sort(a+,a++n,cmp);
for(int i=;i<=n;i++)
{
if(a[i].t<=q.size())
{
if(a[i].m>q.top())
{
ans-=q.top();
q.pop();
q.push(a[i].m);
ans+=a[i].m;
}
}
else
{
q.push(a[i].m);
ans+=a[i].m;
}
}
printf("%lld\n",ans);
return ;
}

Work Scheduling(带反悔的贪心)的更多相关文章

  1. 带"反悔"的贪心-超市

    题面:https://www.acwing.com/problem/content/description/147/ 超市里有N件商品,每个商品都有利润pi和过期时间di,每天只能卖一件商品,过期商品 ...

  2. [Usaco 2012 Feb]Cow coupons牛券:反悔型贪心

    Description Farmer  John  needs  new  cows! There  are  N  cows  for  sale (1 <= N <= 50,000), ...

  3. salesman,动态规划带一点点贪心。

    题目直接链接 分析一下: 这题题意还是比较明白的(少见的一道中文题),他的意思就是:有这么一个无向图:保证联通且点与点直接有唯一的简单路径(说白了就是棵树,根节点是1),每个节点有一个权值(有正有负) ...

  4. 【NOIP模拟赛】就 反悔贪心

    biubiu~~~ 这道题,考场上上来就dp然后发现怎么优化也不行.............最后发现是贪心............. 正解:带反悔的贪心,原理是,假设我们现在得到了取i个的最优解那么我 ...

  5. BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)

    dp容易想到,但没法进一步优化了. 考虑贪心,每次选出价值最大的物品.但这显然是不对的因为会影响其他物品的选择. 于是考虑加上反悔操作.每次选出一个物品后,将其相邻两物品删除,再将原物品价值变为相邻两 ...

  6. [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)

    [USACO09OPEN] 工作调度Work Scheduling 题意翻译 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^ ...

  7. [CSP-S模拟测试]:trade(反悔贪心)

    题目传送门(内部题62) 输入格式 第一行有一个整数$n$.第二行有$N$个整数:$a_1\ a_2\ a_3\cdot\cdot\cdot a_n$. 输出格式 一行一个整数表示最大收益. 样例 样 ...

  8. 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...

  9. BZOJ_2151_种树_贪心+堆+链表

    BZOJ_2151_种树_贪心+堆 Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编 ...

随机推荐

  1. ETL工具对比

    ETL工具对比 Informatica Kettle 起源 1993年创立于 (美国加利福尼亚州)并于1999年4月在纳斯达克上市 2006年加入了开源BI组织  自2017年9月起,已被(日立集团下 ...

  2. MessageBox.Show的使用

    MessageBox.Show("内容","标题") // 摘要:// 使用指定的帮助文件.HelpNavigator 和帮助主题显示一个具有指定文本.标题.按 ...

  3. Vuex 是什么

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具  ...

  4. codeigniter框架开发技巧

    codeigniter框架开发技巧 来源:未知    时间:2014-10-20 11:36   阅读数:171   作者:xbdadmin [导读] (1)自动加载modelsphp5风格 使用这个 ...

  5. UML-架构分析-阶段

    初始阶段:架构概念验证原型--->确定其可行性 细化阶段:因素表.技术备忘录.SAD(软件架构文档) 移交阶段:可能会修改SAD->确保与最终部署版本的一致性 后续进化循环:重温架构性因素 ...

  6. Redhat7 开机启动服务

    #!/bin/sh ### BEGIN INIT INFO # Provides: jboss # Required-Start: $local_fs $remote_fs $network $sys ...

  7. 黑客攻防技术宝典web实战篇:测试后端组件习题答案

    随书答案. 某网络设备提供用于执行设备配置的 Web 界面.为什么这种功能通常易于受 到操作系统命令注入攻击? 用于配置网络设备的应用程序通常包含使用正常的 Web 脚本 API 无法轻松实 现的功能 ...

  8. h5-边框图片

    1.边框图片详解 <style> *{ ; ; } div{ width: 900px; height: 900px; margin: 100px auto; border: 133px ...

  9. PAT Basic 1017 A除以B (20) [数学问题-⼤整数运算]

    题目 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成⽴. 输⼊格式: 输⼊在1⾏中依次给出A和B,中间以1空格分隔. ...

  10. UML-类图-如何画引用类和集合?

    1.画引用类 引用到了类时,需要画关联线,否则其他基本类型(int.string.date等)不画. 2.画集合 当然,方法1中可加入箭头.对应java代码: public class Sale { ...