Work Scheduling(带反悔的贪心)
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(带反悔的贪心)的更多相关文章
- 带"反悔"的贪心-超市
题面:https://www.acwing.com/problem/content/description/147/ 超市里有N件商品,每个商品都有利润pi和过期时间di,每天只能卖一件商品,过期商品 ...
- [Usaco 2012 Feb]Cow coupons牛券:反悔型贪心
Description Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), ...
- salesman,动态规划带一点点贪心。
题目直接链接 分析一下: 这题题意还是比较明白的(少见的一道中文题),他的意思就是:有这么一个无向图:保证联通且点与点直接有唯一的简单路径(说白了就是棵树,根节点是1),每个节点有一个权值(有正有负) ...
- 【NOIP模拟赛】就 反悔贪心
biubiu~~~ 这道题,考场上上来就dp然后发现怎么优化也不行.............最后发现是贪心............. 正解:带反悔的贪心,原理是,假设我们现在得到了取i个的最优解那么我 ...
- BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)
dp容易想到,但没法进一步优化了. 考虑贪心,每次选出价值最大的物品.但这显然是不对的因为会影响其他物品的选择. 于是考虑加上反悔操作.每次选出一个物品后,将其相邻两物品删除,再将原物品价值变为相邻两 ...
- [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)
[USACO09OPEN] 工作调度Work Scheduling 题意翻译 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^ ...
- [CSP-S模拟测试]:trade(反悔贪心)
题目传送门(内部题62) 输入格式 第一行有一个整数$n$.第二行有$N$个整数:$a_1\ a_2\ a_3\cdot\cdot\cdot a_n$. 输出格式 一行一个整数表示最大收益. 样例 样 ...
- 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...
- BZOJ_2151_种树_贪心+堆+链表
BZOJ_2151_种树_贪心+堆 Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编 ...
随机推荐
- 在Linux下 MySQL错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 解决办法【很管用】
一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...
- 实验4&5
[实验任务四]: 在上网时,我们经常会看到以下这种对话框,要用户输入一个验证码. 1.程序设计思想 先利用Math.random()得到一个整数,然后将其类型转换为字符类型,连接起来生成六位验证字符串 ...
- Java算法练习——两数之和
题目链接 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...
- CentOS 7 连接不到网络解决方法
使用VM12创建虚拟机并安装CentOS 7,但是安装完成后发现连接不到网络. ping jd.com发现不通 因为在创建虚拟机的时候我们选择的是NAT模式 这里给出NAT模式下对应的的解决方法: 一 ...
- FTP服务器 vsftp samba服务器 共享 smb
FTP服务器 vsftp samba服务器 共享 smb 马哥视频 参考1 参考3 参考2 参考4 vsftp服务器实现匿名用户上传.修改权限和一些设置 win7访问 地址栏输入 ftp://账号 ...
- 8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系
一. PropTypes 与 DefaultProps 官方文档 1. PropTypes 属性校验 引入 PropTypes import PropTypes from 'prop-types'; ...
- Nacos快速开始
Nacos是一个服务发现.配置管理和服务管理的组件. 说到服务注册与发现,我想到Eureka.Zookeeper 说到服务治理,我想到Dubbo 说到配置管理,我想到Apollo 作为后起之秀的Nac ...
- [De1CTF 2019]SSRF Me-MD5长度扩展攻击&CVE-2019-9948
0x00 打开题目查看源代码,开始审计 这里贴上网上师傅的博客笔记: https://xz.aliyun.com/t/6050 #! /usr/bin/env python #encoding=utf ...
- CI项目设计Redis队列
项目开发过程中需要设计提供可平衡的处理多个用户请求的队列. 需求: 当用户登录后,查看系统中已经登录的管理员队列,然后查看后台管理员的处理能力,如果已经不能处理新的请求,则把该管理员从处理队列中删除, ...
- 吴裕雄--天生自然TensorFlow高层封装:Estimator-自定义模型
# 1. 自定义模型并训练. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist i ...