UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成。第一项任务开始的时间不早于时刻0。
分析:按截止时间排序,在若当前工作完成后超过截止时间,则优先选择需要时间少的工作,即若优先队列中需要时间最长的工作a,
1、其时间大于当前工作b,则选择完成工作b,弹出a。(此操作的前提是完成b弹出a后的时间要小于等于b的截止时间)
2、其时间小于当前工作b,则没必要弹出a。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
if(fabs(a - b) < eps) return 0;
return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 800000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Node{
int q, d;
void read(){
scanf("%d%d", &q, &d);
}
bool operator < (const Node& rhs)const{
return d < rhs.d;
}
}num[MAXN];
int n;
int solve(){
priority_queue<int> q;
int st = 0;
for(int i = 0; i < n; ++i){
if(st + num[i].q <= num[i].d){
st += num[i].q;
q.push(num[i].q);
}
else if(!q.empty()){
int tmp = q.top();
if(tmp > num[i].q && st - tmp + num[i].q <= num[i].d){
q.pop();
q.push(num[i].q);
st += num[i].q - tmp;
}
}
}
return q.size();
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i) num[i].read();
sort(num, num + n);
printf("%d\n", solve());
if(T) printf("\n");
}
return 0;
}
UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)的更多相关文章
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...
- UVA - 1153 Keep the Customer Satisfied(贪心)
UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: ...
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...
- UVA 1153 KEEP THE CUSTOMER SATISFIED
题意: 钢铁公司有N个客户的订单,每个订单有一个产量q(生产时间刚好也等于q)和订单完成截止时间.公司要求完成尽量多的订单. 分析: 先按截止时间d排序,然后维护一个已经选好的订单的优先队列,如果当前 ...
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序, ...
- 【uva 1153】Keep the Customer Satisfied(算法效率--贪心+优先队列)
题意:有N个工作,已知每个工作需要的时间和截止时间.要求所有工作穿行完成,第一项任务开始的时间不早于时刻0.问最多能完成多少个工作.(N≤800000) 解法:贪心.可以模型化题目为:已知N个任务的长 ...
- uva 1153 顾客是上帝(贪心)
uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...
- poj 2786 - Keep the Customer Satisfied
Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...
- UVA1153-Keep the Customer Satisfied(贪心)
Problem UVA1153-Keep the Customer Satisfied Accept: 222 Submit: 1706Time Limit: 3000 mSec Problem D ...
随机推荐
- Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...
- priority_queue优先级队列总结
http://www.cppblog.com/Darren/archive/2009/06/09/87224.html priority_queue用法 priority_queue 调用 STL里面 ...
- 输入、输出(iostream)
在一个程序当中输入和输出都扮演着重要的角色,所以掌握基本输入输出是入门一门语言所必不可少的.本文主要简单叙述java的输入和输出. package ios; import java.util.Scan ...
- Unity Scene视图下 输出物体坐标等信息
using UnityEditor; using UnityEngine; [CustomEditor(typeof(GameObject))] public class MyEditor : Edi ...
- 侧边栏下拉时箭头的旋转动画(treeView控件)
//点击菜单时箭头旋转 let treeView = document.getElementsByClassName("treeview");//let解决闭包问题 let las ...
- NO31 配置网卡--主机名--网络故障排查面试题--DNS
修改网卡配置信息: 修改主机名规范的三个步骤: 配置默认网关: DNS解析过程,用命令看: DNS相关命令: 口述DNS解析过程: 客户端(电脑)通过浏览器输入域名,先找hosts文件及本地dns缓 ...
- DEDE后台升级后不显示编辑器
dede5.7不显示编辑器不能编辑文章的解决办法:进入系统后台系统配置-系统基本参数-核心设置将fck换成ckeditor保存,当然需要fck编辑器也可以到dede官网下载.dede5.7不显示编辑器 ...
- 设备树DTS 学习:1-有关概念
背景 设备树在Linux驱动开发中是一种比较常用的架构. 参考:<设备树DTS使用总结> .<linux内核设备树及编译> Linux设备树 介绍 在Linux 2.6中,ar ...
- bsearch的使用
懒得写二分查找,结果发现stdlib里自带了二分查找. C 库函数 void *bsearch(const void *key, const void *base, size_t nitems, si ...
- 学习进度-04 Scala的学习
在Scala中,主函数的定义是def main(args: Array[String]),Scala中必须使用对象方法 1.变量: Scala中的变量分为两种var和val. 例如:def main( ...