标签:
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).
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.
OutputFor each test case, the output must follow the description below. 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
Gee but it‘s great to be back home Home is where I want to be. I‘ve been on the road so long my friend, And if you came along I know you couldn‘t disagree. It‘s the same old story Everywhere I go, I get slandered, Libeled, I hear words I never heard In the bible And I‘m on step ahead of the shoe shine Two steps away from the county line Just trying to keep my customers satisfied, Satisfied. Deputy sheriff said to me Tell me what you come here for, boy. You better get your bags and flee. You‘re in trouble boy, And you‘re heading into more. ©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[8000010]; 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=0; i<n; i++) scanf("%d%d", &a[i].q, &a[i].d); sort(a,a+n,cmp); int total=0;int s; for(int i=0; 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 0; }
借鉴别人博客的,更简练的程序
#include<iostream> #include<algorithm> #include<stack> #include<queue> #include<cstdio> using namespace std; struct node { int q; int d; }; node a[8000010]; 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=0; i<n; i++) scanf("%d%d", &a[i].q, &a[i].d); sort(a,a+n,cmp); int total=0;int k=0; for(int i=0; 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 0; }
UVA - 1153 Keep the Customer Satisfied(贪心)
标签:
原文地址:http://www.cnblogs.com/hfc-xx/p/4708870.html