UVA1153-Keep the Customer Satisfied(贪心)
Accept: 222 Submit: 1706
Time Limit: 3000 mSec
Problem Description

Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Data Each test case is described by one input file that contains all the relevant data: The first line contains the number n of orders (n can be as large as 800000 for some test cases). It is followed by n lines. Each of which describes an order made of two integer values: the amount of steel (in tons) required for the order (lower than 1000) and its due date (in seconds; lower than 2×106).
Output
Sample Input
6
7 15
8 20
6 8
4 9
3 21
5 22
Sample Output
4
题解:贪心算法,首先按截至时间从小到大排序,这个比较自然,然后贪心加区间,如果能直接加进去,就先加进去,如果不能,就比较该区间的持续时间和目前算进答案的持续时间最长的区间,如果该区间持续时间短,就删去大的,加进小的,正确性还是比较明显的。
#include <bits/stdc++.h>
using namespace std;
const int maxn = + ;
struct Work {
int q, d;
Work() {}
Work(int _q, int _d) : q(_q), d(_d) {}
bool operator < (const Work &a)const {
if (a.d == d) return q < a.q;
else return d < a.d;
}
}work[maxn];
int n;
int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
bool flag = false;
while (iCase--) {
scanf("%d", &n);
if (flag) printf("\n");
flag = true;
int cnt = ;
for (int i = ; i < n; i++) {
scanf("%d %d", &work[i].q, &work[i].d);
}
sort(work, work + n);
int ans = , pos = ;
priority_queue<int> que;
for (int i = ; i < n; i++) {
if (pos + work[i].q <= work[i].d) {
pos += work[i].q;
que.push(work[i].q);
ans++;
}
else if(!que.empty()){
int top = que.top();
if (top > work[i].q) {
pos += work[i].q - top;
que.pop();
que.push(work[i].q);
}
}
}
printf("%d\n", ans);
}
return ;
}
UVA1153-Keep the Customer Satisfied(贪心)的更多相关文章
- UVA-1153 Keep the Customer Satisfied (贪心)
题目大意:有n件工作,做每件工作的消耗时间为s,截止时间为d,问最多能做完几件工作. 题目分析:贪心策略:优先做截止时间靠前的,一旦做不完当前工作,则从已经做过的工作中删去一件耗时最长的,用当前工作取 ...
- UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)
VJ题目链接 题意: 知道n(n <= 8e6)个工作的完成所需时间q和截止时间d,你一次只能做一个工作.问最多能做多少工作? 思路: 首先很像贪心.观察发现如下两个贪心性质: 1)一定存在一个 ...
- uva1153 Keep the Customer Satisfied
贪心加优先队列 (默认是小的在前,正好) //这里又很套路,设队列里的都是符合条件的考虑新加入的即可.再处理一下空队列的情况.很完美// 截止时间短的在前面,干的就多先根据截止日期排序优先队列根据完成 ...
- UVA - 1153 Keep the Customer Satisfied(贪心)
UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: ...
- poj 2786 - Keep the Customer Satisfied
Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序, ...
- 【uva 1153】Keep the Customer Satisfied(算法效率--贪心+优先队列)
题意:有N个工作,已知每个工作需要的时间和截止时间.要求所有工作穿行完成,第一项任务开始的时间不早于时刻0.问最多能完成多少个工作.(N≤800000) 解法:贪心.可以模型化题目为:已知N个任务的长 ...
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...
随机推荐
- JavaScript 笔记(一)
Number 1.2345e3=1.2345*1000 NaN//not a num 无法计算结果 Infinity //无限大 数组 var arr=[1,2,'hello'] 下标从0开始 对象 ...
- JavaScript是如何工作的:Service Worker的生命周期及使用场景
摘要: 理解Service Worker. 原文:JavaScript 是如何工作的:Service Worker 的生命周期及使用场景 作者:前端小智 Fundebug经授权转载,版权归原作者所有. ...
- thinkphp模板中,checkbox回显问题
thinkphp 模板里面可以这样写包含操作 //in 标签 <in name="变量名" value="值1,值2,...">要输出的内容< ...
- cf1139D. Steps to One(dp)
题意 题目链接 从\([1, M]\)中随机选数,问使得所有数gcd=1的期望步数 Sol 一个很显然的思路是设\(f[i]\)表示当前数为\(i\),期望的操作轮数,转移的时候直接枚举gcd \(f ...
- 2018-11-16 中文代码示例之Programming in Scala笔记第四五六章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章. 同样仅节选有意思的例程部分作演示之用. 源文档仍在: program-in-chinese/Programming_ ...
- Javascript 流程控制
流程控制 JavaScript通过流程语句来执行程序流,程序流有若干语句组成.在正常情况下,程序中 的语句时按照书写顺序执行的,这种结构称为顺序结构.除了顺序结构之外还有选择结构和循环结构. 1.选择 ...
- filter帅选
var ages = [32, 33, 16, 40]; ages= ages.filter(function checkAdult(obj) {//obj表示数组中的每个元素 return obj ...
- 深圳共创力“研发管理&知识管理”高端研讨交流会在深圳举办!
2017/4/8,由深圳市共创力企业管理咨询公司举办的“研发管理&知识管理”高端研讨会在深圳市南山区圣淘沙国际酒店(翡翠店)隆重召开.此次研讨会由共创力总经理.首席顾问杨学明先生主持.研讨会先 ...
- Java:字节流和字符流(输入流和输出流)
本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...
- Spark性能优化【Stack Overflow】
一.异常情况 Stack Overflow 二.异常分析 之所以会产生Stack Overflow,原因是在Stack方法栈中方法的调用链条太长的原因导致的,一般情况有两种: 1.过于深度的递归[常见 ...