题目大意:有n件工作,做每件工作的消耗时间为s,截止时间为d,问最多能做完几件工作。

题目分析:贪心策略:优先做截止时间靠前的,一旦做不完当前工作,则从已经做过的工作中删去一件耗时最长的,用当前工作取代之。

代码如下:

# include<iostream>
# include<cstdio>
# include<vector>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; struct Work
{
int s,t;
Work(int _s,int _t):s(_s),t(_t){}
bool operator < (const Work &a) const {
return s<a.s;
}
};
vector<Work>w;
priority_queue<Work>q; bool myComp(const Work &a,const Work &b)
{
return a.t<b.t;
} int solve(int n)
{
while(!q.empty()) q.pop();
int ans=0,t=0;
for(int i=0;i<n;++i){
t+=w[i].s;
++ans;
q.push(w[i]);
if(t>w[i].t){
Work u=q.top();
q.pop();
t-=u.s;
--ans;
}
}
return ans;
} int main()
{
int T,s,t,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
w.clear();
for(int i=0;i<n;++i){
scanf("%d%d",&s,&t);
w.push_back(Work(s,t));
}
sort(w.begin(),w.end(),myComp);
printf("%d\n",solve(n));
if(T)
printf("\n");
}
return 0;
}

  

UVA-1153 Keep the Customer Satisfied (贪心)的更多相关文章

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

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

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

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

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

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

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

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

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

    题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...

  6. UVA 1153 KEEP THE CUSTOMER SATISFIED

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

  7. uva 1153 顾客是上帝(贪心)

    uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...

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

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

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

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

  10. poj 2786 - Keep the Customer Satisfied

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

随机推荐

  1. os模块os.walk() 方法和os.path.join()的简单使用

    os.walk:   http://www.runoob.com/python/os-walk.html os.path.join:   https://blog.csdn.net/zmdzbzbhs ...

  2. 20144306《网络对抗》MAL_后门原理与实践

    本期收获 1.了解后门的基本概念. 2.Netcat.socat.MSF meterpreter的使用(MSF meterpreter实在太好玩了) 3.后门软件的启动方式: Windows任务计划程 ...

  3. Highway Project---zoj3946(最短路SPFA)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 题意: 有n个点 m(n,m<=10^5)条路,现在要建 ...

  4. Flask系列之蓝图中使用动态URL前缀

    让我们先来看一个简单的例子,假设有下面这样一个蓝图(是关于用户主页的): from flask import Blueprint, render_template profile = Blueprin ...

  5. ajax请求,html调用js

    1:html中调用js中的函数,js使用ajax请求向后台请求,返回数据. <!DOCTYPE html> <html lang="en"> <hea ...

  6. 【Lua】面向对象编程(二)

      多重继承: module(...,package.seeall) local function search(k,plist) ,#plist do local v=plist[i][k] if ...

  7. Qt工程文件说明

    Qt工程文件说明 2017-10-16  天天快乐6...  转自 LZS2851 修改   微信 分享: 这篇文章是我从360doc上转的,本来是要把转的url列出来的,但是它们居然禁掉了复制,而且 ...

  8. Linux系统——Inotify事件监控工具

    每秒传输文件200个 Rsync放在定时任务中也只是一分钟执行一回,要想达到实时的效果,为防止单点nfs架构故障,再启动一台nfs服务器作为主nfs服务器的备份服务器,此时需要inotify实时同步数 ...

  9. #C++初学记录(素数判断)

    练习题目二 素数判断 A prime number is a natural number which has exactly two distinct natural number divisors ...

  10. js 中的[] {}是什么意思

    <div id="aa1">aaaaa</div><div id="aa2">bbbb</div><div ...