UVA - 1153 Keep the Customer Satisfied(贪心)
UVA - 1153
Description ![]() Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of the major objective of Paul and Art, the managers. Customers issue orders that are characterized by two integer values q<tex2html_verbatim_mark> , the amount of steel required (in tons) and d<tex2html_verbatim_mark> , the due date (a calender date converted in seconds). The due date has to be met if SG Corp. accepts the order. Stated another way, when an order is accepted, the corresponding amount of steel has to be produced before its due date. Of course, the factory can process no more than one order at a time. Although the manufacturing process is rather complex, it can be seen as a single production line with a constant throughput. In the following, we assume that producing q<tex2html_verbatim_mark> tons of steel takes exactly q<tex2html_verbatim_mark> seconds (i.e., throughput is 1). The factory runs on a monthly production plan. Before the beginning of the month, all customers' orders are collected and Paul and Art determine which of them are going to be accepted and which ones are to be rejected in the next production period. A production schedule is then designed. To keep customers satisfied, Paul and Art want to minimize the total number of orders that are rejected. In the following, we assume that the beginning of the next production plan (i.e., the first day of the next month) corresponds to date 0. Hogdson and Moore have been appointed as Chief Scientific Officers and you are requested to help them to compute an optimal solution and to build a schedule of all accepted orders (starting time and completion time). Small Example Consider the following data set made of 6 orders J1,..., J6<tex2html_verbatim_mark> . For a given order, Jj<tex2html_verbatim_mark> , qj<tex2html_verbatim_mark> denotes the amount of steel required and dj<tex2html_verbatim_mark> is the associated due date.
You can check by hand that all orders cannot be accepted and it's very unlikely you could find a solution with less than two rejected orders. Here is an optimal solution: Reject J1<tex2html_verbatim_mark> and J4<tex2html_verbatim_mark> , accept all other orders and process them as follows.
Note that the production line is never idle. InputThe 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<tex2html_verbatim_mark> of orders ( n<tex2html_verbatim_mark> can be as large as 800000 for some test cases). It is followed by n<tex2html_verbatim_mark> 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 x 106<tex2html_verbatim_mark> ). OutputFor each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. You are required to compute an optimal solution and your program has to write the number of orders that are accepted. Sample Input1 6 Sample Output4 Some Hints from Hogdson and Moore
Keep the Customer Satisfied Gee but it's great to be back home ©Simon & Garfunkel |
解题报告: 题中基本告诉我们怎么做这题了。首先按照截止时间的先后排序。对于任意两个任务a和b,如果a的截止时间在b之前,且a的加工时间比b长,那么接受了a订单必然要接受b订单。反过来呢,如果b的加工时间超过了截止时间,那么就找之前的订单,删掉加工时间最长的那个订单。这样接受的订单数没有变化,而总的加工时间变短了,为以后接受更多订单做准备。总要拒绝一些订单的,所以用优先队列维护q
代码:
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstdio>
using namespace std;
struct node
{
int q;
int d;
};
node a[];
int cmp(node x,node y)
{
return x.d<y.d;
}
int main()
{
int t,n;;
cin>>t;
while(t--)
{
scanf("%d",&n);
priority_queue<int>p;
for(int i=; i<n; i++)
scanf("%d%d", &a[i].q, &a[i].d);
sort(a,a+n,cmp);
int total=;int s;
for(int i=; i<n; i++)
{
if(a[i].q+total<=a[i].d)
{
p.push(a[i].q);
total+=a[i].q;
}
else if(!p.empty())
{
s=p.top();
if(s>a[i].q)
{
total=total-s+a[i].q;
p.pop();
p.push(a[i].q);
}
}
}
printf("%d\n",p.size());
if(t) cout<<endl;
}
return ;
}
借鉴别人博客的,更简练的程序
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstdio>
using namespace std;
struct node
{
int q;
int d;
};
node a[];
int cmp(node x,node y)
{
return x.d<y.d;
}
int main()
{
int t,n;;
cin>>t;
while(t--)
{
scanf("%d",&n);
priority_queue<int>p;
for(int i=; i<n; i++)
scanf("%d%d", &a[i].q, &a[i].d);
sort(a,a+n,cmp);
int total=;int k=;
for(int i=; i<n; i++)
{
total+=a[i].q;
p.push(a[i].q);
if(total>a[i].d)
{
total-=p.top();
p.pop();
k++;
} }
printf("%d\n",n-k);
if(t) cout<<endl;
}
return ;
}
UVA - 1153 Keep the Customer Satisfied(贪心)的更多相关文章
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序, ...
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...
- UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...
- UVA 1153 KEEP THE CUSTOMER SATISFIED
题意: 钢铁公司有N个客户的订单,每个订单有一个产量q(生产时间刚好也等于q)和订单完成截止时间.公司要求完成尽量多的订单. 分析: 先按截止时间d排序,然后维护一个已经选好的订单的优先队列,如果当前 ...
- uva 1153 顾客是上帝(贪心)
uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...
- UVALive 3507:Keep the Customer Satisfied(贪心 Grade C)
VJ题目链接 题意: 知道n(n <= 8e6)个工作的完成所需时间q和截止时间d,你一次只能做一个工作.问最多能做多少工作? 思路: 首先很像贪心.观察发现如下两个贪心性质: 1)一定存在一个 ...
- UVA1153-Keep the Customer Satisfied(贪心)
Problem UVA1153-Keep the Customer Satisfied Accept: 222 Submit: 1706Time Limit: 3000 mSec Problem D ...
- poj 2786 - Keep the Customer Satisfied
Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousa ...
随机推荐
- 金牌分析师助力 鲁泰A图谋再造一个“鲁泰”?_财经_中国网
金牌分析师助力 鲁泰A图谋再造一个"鲁泰"?_财经_中国网 金牌分析师助力 鲁泰A图谋再造一个"鲁泰"?
- B - Dungeon Master
题目大意: 地牢大师(感觉像是一款游戏啊.......) 你被困在一个3D的地牢里面,并且需要发现最快的出去的路,这个地牢由很多小立方体组成,有的是空的可以走,有的被岩石填充了不可以走,移动一 ...
- Directx 3D编程实例:随机绘制的立体图案旋转
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...
- Maven deploy
1.在maven/conf/setting.xml中: 在<servers>中加入 <server> <id>internal</id> <u ...
- BIGINT UNSIGNED value is out of range in … 问题的解决方法
问题出现在CAST(value AS USIGNED)将字符串转换成数值的过程中,出现这个问题的原因是value对应的数值在BIGINT UNSIGNED 的范围内.可能的情况是value的值太大,超 ...
- STRUCTS 2 LABLE
{LJ?Dragon}[标题]structs2标签的作用 {LJ?Dragon}[Diary]2017年,愉快的开始:离别不一定总伤感,虽然只是安慰着自己......... 问与答 问题 在Strut ...
- 双系统如何正确的使用修复BCD工具分享
安装双系统时候,用于种种原因会导致开机启动只显示一个系统,此时需要修复下BCD即可. 下面介绍下两个修复BCD工具软件: 1.easybcd(双系统引导修复工具) v2.2.0.182 汉化版 下载地 ...
- 设置IIS7文件上传的最大大小 maxAllowedContentLength,maxRequestLength
当上传一个超过30M的文件时,服务器会重定向至404.13页面,报错如下: HTTP Error 404.13 - Not Found The request filtering module is ...
- 分享一个linux和linux的文件传输【scp无密码传输】
很多时候,本地测试服务器想把文件传到线上服务器的时候,很多人都是通过登陆线上服务器ssh 传输,这样挺危险的,很多弊端....所以我找了下方法,发现scp挺好用的! 模拟环境: 192.168.147 ...
- springMVC3学习(二)--ModelAndView对象
当控制器处理完请求时,一般会将包括视图名称或视图对象以及一些模型属性的ModelAndView对象返回到DispatcherServlet. 因此,常常须要在控制器中构造ModelAndView对象. ...