贪心加优先队列 (默认是小的在前,正好)

//这里又很套路,设队列里的都是符合条件的考虑新加入的即可。再处理一下空队列的情况。很完美//

截止时间短的在前面,干的就多
先根据截止日期排序
优先队列根据完成所需时间排序
首先队列里的都是能完成的
策略:新加入的,如果在前面的完成后仍能完成,就直接加进去;不能,因为截止日期在更后,不影响其他的,比较top元素用的时间,更短就换掉(此时因为截止日期靠后,用的时间还短,所以能成立)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int MAXN = ;
  10. const int N = ;
  11.  
  12. struct order{
  13. int q, d;
  14. bool operator < (const order &a) const {
  15. return q<a.q;
  16. }
  17. }o[MAXN];
  18.  
  19. int cmp (order a, order b) {
  20. return a.d < b.d;
  21. }
  22.  
  23. int n;
  24. int solve() {
  25. priority_queue<int> done;
  26. int sum = , temp;
  27. for (int i = ; i < n; i++) {
  28. if (o[i].q + sum <= o[i].d) {
  29. done.push(o[i].q);
  30. sum += o[i].q;
  31. }
  32. else if (!done.empty()){
  33. temp = done.top();
  34. if (temp > o[i].q) {
  35. sum = sum - temp + o[i].q;
  36. done.pop();
  37. done.push(o[i].q);
  38. }
  39. }
  40. }
  41. return done.size();
  42. }
  43.  
  44. int main() {
  45. int T;
  46. scanf("%d", &T);
  47. while (T--) {
  48. scanf("%d", &n);
  49. for (int i = ; i < n; i++)
  50. scanf("%d%d", &o[i].q, &o[i].d);
  51. sort(o, o + n, cmp);
  52. printf("%d\n", solve());
  53. if(T)
  54. printf("\n");
  55. }
  56. return ;
  57. }

uva1153 Keep the Customer Satisfied的更多相关文章

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

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

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

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

  3. poj 2786 - Keep the Customer Satisfied

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

  4. UVA1153-Keep the Customer Satisfied(贪心)

    Problem UVA1153-Keep the Customer Satisfied Accept: 222  Submit: 1706Time Limit: 3000 mSec Problem D ...

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

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

  6. Keep the Customer Satisfied

    题意: n个订单,每个订单有完成需要的天数,和限制的天数,求最多能完成多少订单 分析: 先按限制日期升序排列,若当前订单不能完成,和上面已选中的订单中需要天数中最大的比较,若比它小,则替换他. #in ...

  7. UVA 1153 KEEP THE CUSTOMER SATISFIED

    题意: 钢铁公司有N个客户的订单,每个订单有一个产量q(生产时间刚好也等于q)和订单完成截止时间.公司要求完成尽量多的订单. 分析: 先按截止时间d排序,然后维护一个已经选好的订单的优先队列,如果当前 ...

  8. UVALive - 3507 Keep the Customer Satisfied

    题意:收到n个订单,每个订单有q,d分别代表做这个的时间,和最晚的完成时间,问你最多能接受几个订单 思路:贪心,我们显然要按最早的完成时间排序,那么接下来,我们用(6,8)和(4,9)做为例子,按照我 ...

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

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

随机推荐

  1. GYM 100741A Queries

    传送门 题目大意: 一个长度为n的序列,q次三种操作 +p  r:下标为p的数+r -p r:下标为p的数-r s l r mod [L,R]中有多少数%m=mod,m已经给出 题解: 开十个树状数组 ...

  2. cas单点登录系统:客户端(client)详细配置(包含统一单点注销配置)

    最近一直在研究cas登录中心这一块的应用,分享一下记录的一些笔记和心得.后面会把cas-server端的配置和重构,另外还有这几天再搞nginx+cas的https反向代理配置,以及cas的证书相关的 ...

  3. 从零开始构建一个Reactor模式的网络库(一) 线程同步Mutex和Condition

    最近在学习陈硕大神的muduo库,感觉写的很专业,以及有一些比较“高级”的技巧和设计方式,自己写会比较困难. 于是打算自己写一个简化版本的Reactor模式网络库,就取名叫mini吧,同样只基于Lin ...

  4. A. Hulk

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. 从开发的角度对zigbee安全的杂谈

    说起zigbee应该很少人听过,这个B名字怪怪的... 以前开发不懂开发的思想,前前后后花了很久时间,现在回想起来,突然想从安全的角度来理解数据的传输 废话:伴随科技的快速演进,物联网(The Int ...

  6. [转]C# Socket编程笔记

    本文转自:http://www.cnblogs.com/stg609/archive/2008/11/15/1333889.html 原文如下: 看到这个题目,是不是很眼熟?在博客园里搜下,保证会发现 ...

  7. ASP.NET Core Web API + Angular 仿B站(一) 目的分析以及创建 WebAPI + Angular7 项目

    前言: 本系列文章主要为对所学 Angular 框架的一次微小的实践,对 b站页面作简单的模仿. 本系列文章主要参考资料: 微软文档: https://docs.microsoft.com/zh-cn ...

  8. 704. Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  9. bzoj 1385: [Baltic2000]Division expression【脑洞】

    加括号再去括号就是除变加,显然尽可能多的除变加是最优的,然后发现唯一不能变成乘数的是第二个数,所以把其他数乘起来mod第二个数,如果是0就是YES,否则说明最后不能除尽,就是NO #include&l ...

  10. LuoguP1268树的重量【构造/思维】By cellur925

    题目传送门 Description 给你一个矩阵$M$,$M(i,j)$表示$i$到$j$的最短距离.定义树的重量为树上各边权之和,对于任意给出的合法矩阵$M$,已知它所能表示树的重量是唯一确定的.给 ...