Problem 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

For each test case, you are required to compute an optimal solution and your program has to write the number of orders that are accepted. The outputs of two consecutive cases will be separated by a blank line.
 

 Sample Input

1
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(贪心)的更多相关文章

  1. UVA-1153 Keep the Customer Satisfied (贪心)

    题目大意:有n件工作,做每件工作的消耗时间为s,截止时间为d,问最多能做完几件工作. 题目分析:贪心策略:优先做截止时间靠前的,一旦做不完当前工作,则从已经做过的工作中删去一件耗时最长的,用当前工作取 ...

  2. UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)

    VJ题目链接 题意: 知道n(n <= 8e6)个工作的完成所需时间q和截止时间d,你一次只能做一个工作.问最多能做多少工作? 思路: 首先很像贪心.观察发现如下两个贪心性质: 1)一定存在一个 ...

  3. uva1153 Keep the Customer Satisfied

    贪心加优先队列 (默认是小的在前,正好) //这里又很套路,设队列里的都是符合条件的考虑新加入的即可.再处理一下空队列的情况.很完美// 截止时间短的在前面,干的就多先根据截止日期排序优先队列根据完成 ...

  4. UVA - 1153 Keep the Customer Satisfied(贪心)

    UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: ...

  5. poj 2786 - Keep the Customer Satisfied

    Description   Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...

  6. UVa 1153 Keep the Customer Satisfied (贪心+优先队列)

    题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序, ...

  7. 【uva 1153】Keep the Customer Satisfied(算法效率--贪心+优先队列)

    题意:有N个工作,已知每个工作需要的时间和截止时间.要求所有工作穿行完成,第一项任务开始的时间不早于时刻0.问最多能完成多少个工作.(N≤800000) 解法:贪心.可以模型化题目为:已知N个任务的长 ...

  8. UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】

    题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...

  9. UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)

    因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...

随机推荐

  1. 小兔的棋盘(hdu2067)

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. hive -f 传递参数

    hive -f 在执行sql脚本文件的时候是可以传递参数的,但是要注意hive版本: 注意:hive在0.9版本之前是不支持-f传递参数的,只有1.0之后才支持次功能. 使用如下: 1.创建sql脚本 ...

  3. js对象工厂函数与构造函数

    转自:http://www.cnblogs.com/Jener/p/5920963.html ★概述:         使用对象字面量,或者向空对象中动态地添加新成员,是最简单易用的对象创建方法.然而 ...

  4. Debug始于71年前

    摘要: 纪念Grace Hopper发现世界上第一个计算机BUG! 1947年9月9日,Grace Hopper的计算科学团队在哈佛的哈弗Mark II电脑运行程序时遇到一个技术故障.她在发生故障的M ...

  5. mybatis全局属性(全局变量)

    mybatis全局属性(全局变量):方法1:在 properties 元素体内,使用<property>标签定义的属性方法2:在 properties 元素中, 使用 resource 或 ...

  6. idea代码提示

    idea代码提示:Keymap-->Main menu-->Code-->Completion去掉Cyclic Expand Word的快捷键将Basic的快捷键更改为Alt+/

  7. mac gulp: command not found

    mac下执行gulp的时候报错:gulp: command not found 1.查看npm的安装目录 npm root 2.如果不是/usr/local , 说明未全局安装,执行 sudo npm ...

  8. Dynamics 365 Online-Unified User Interface

    为了加强界面的一致性,提高用户体验,Dynamics 365 Online V9发布了新的Interface:Unified User Interface. 区别于旧的Regular UI,UUI自适 ...

  9. 使用JDBC连接MySQL数据库的一个基本案例

    JDBC的概念(摘自百度百科) JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一 ...

  10. 导入MySQL数据库提示"Unknown character set: 'utf8mb4'"错误

      错误提示:导入MySQL数据库提示"Unknown character set: 'utf8mb4'"错误   分析: 看来是因为数据库版本的问题导致的,之前网站MYSQL5. ...