UVA - 1153 Keep the Customer Satisfied(贪心)

UVA - 1153

Keep the Customer Satisfied

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

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.

Order qj<tex2html_verbatim_mark> dj<tex2html_verbatim_mark>
J1<tex2html_verbatim_mark> 6 8
J2<tex2html_verbatim_mark> 4 9
J3<tex2html_verbatim_mark> 7 15
J4<tex2html_verbatim_mark> 8 20
J5<tex2html_verbatim_mark> 3 21
J6<tex2html_verbatim_mark> 5 22

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.

Accepted Order Starting Time Completion Time
J2<tex2html_verbatim_mark> 0 4
J3<tex2html_verbatim_mark> 4 11
J5<tex2html_verbatim_mark> 11 14
J6<tex2html_verbatim_mark> 14 19

Note that the production line is never idle.

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<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> ).

Output

For 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 Input

1

6
7 15
8 20
6 8
4 9
3 21
5 22

Sample Output

4

Some Hints from Hogdson and Moore

  • Hogdson and Moore claim that it is optimal to sequence accepted orders in non-decreasing order of due dates.
  • They also claim that there is an optimal solution such that for any two orders Ju<tex2html_verbatim_mark> and Jv<tex2html_verbatim_mark> with qu > qv<tex2html_verbatim_mark> and du < dv<tex2html_verbatim_mark> , if Ju<tex2html_verbatim_mark> is accepted then Jv<tex2html_verbatim_mark> is also accepted.
  • Finally, Hogdson and Moore advise you to ``Keep the Customer Satisfied"

Keep the Customer Satisfied

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;
}
时间: 2024-10-30 00:34:39

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

Uva 1153 Keep the Customer Satisfied (贪心+优先队列)

题意:已知有n个工作,已知每个工作需要的工作时间qi和截至时间di,工作只能串行完成,问最多能完成多少个工作 思路:首先我们按照截至时间从小到大排序,让它们依次进入优先队列中,当发生执行完成时间大于截至时间时,我通过优先队列把工作中最长的需要时间出队 优先队列的比较函数: struct cp { bool operator () (node a,node b) { //可以理解为我如何让后入队的优先出队 if(b.need>a.need) return true; else return fal

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

题意:给定 n 个工作,已知每个工作要用的时间 q 和 截止时间 d,问你最多完成多少个工作,每次最多能运行一个工作. 析:这个题是贪心,应该能看出来,关键是贪心策略是什么,这样想,先按截止时间排序,那么这样,所有的工作就是都是按照截止时间排,因为我们先保证, 截止时间早的先选,然后再从把所有的遍历一下,利用优先队列,q大的优先,然后考虑,后面的,如果后面的还能在截止时间内完成,就放入,如果不能,那么, 和队列中q最长的比,如果比队列中q最长的还长,那么就不要了,否则,那么就删除最长的,把它放进

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

因为每增加一个订单,时间是会增加的,所以先按截止时间d排序,这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单.然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话,那么尝试和之前花费时间最长的交换.如果qi<qj的话,交换之后花费的时间更短且截止时间di更长,情况不会比没交换更糟·. #include<bits/stdc++.h> using namespace std; const int maxn = 8e5+5; struct Cus { int

UVA 1153 KEEP THE CUSTOMER SATISFIED

题意: 钢铁公司有N个客户的订单,每个订单有一个产量q(生产时间刚好也等于q)和订单完成截止时间.公司要求完成尽量多的订单. 分析: 先按截止时间d排序,然后维护一个已经选好的订单的优先队列,如果当前无法选择的话,那么尝试和之前花费时间最长的交换.如果qi<qj的话,交换之后花费的时间更短且截止时间di更长. 代码: #include<iostream>#include<cstdio>#include<algorithm>#include<queue>

1153 - Keep the Customer Satisfied(贪心)

又是一道经典的贪心算法题目 . 乍看题目,想到了紫书一开始讲的区间问题(给定一些区间,选择尽可能多的不相交区间),和另一个经典问题:"活动安排"  的实质是一样的. 但是本题又和区间问题不同,因为区间起点未知,我们所知道的仅仅是等待时间和截至时间,但是其实贪心思想是一致的,即:尽可能的给后面的人留下更多时间,满足当前所用时间最少. 因此可以写出贪心算法 : 按照截至时间排序,将元素的消耗时间加到优先队列里,这样队首元素就是消耗时间最长的人,如果加上当前的人时间超过了他的截至时间,那么将

UVa1153 Keep the Customer Satisfied (贪心,优先队列)

链接:http://bak.vjudge.net/problem/UVA-1153 分析:将n个工作按截止时间d从小到大排序.用优先队列维护在当前截止时间di下,实现完成工作数的最大化时,选取的各个工作的需要时间.每次取一个工作i,先把它扔进队列中(这样保证n个工作都进过队列,在后面我们只需记录下从队列里删除的工作总数ans),计算得到cost(在截止时间di-1之前完成最大工作数所耗用的总时间)加上di的总耗时,如若超过了当前截止时间di,那么根据贪心的思想,从队列里删除一个最费时的工作ans

UVA1153-Keep the Customer Satisfied(贪心)

Problem UVA1153-Keep the Customer Satisfied Accept: 222  Submit: 1706Time 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 des

uva:10700 - Camel trading(贪心)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这样的表达式加上括号后能得到的最大值和最小值. 解题思路:因为这些数的都是正整数,所以可以用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 因为比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> cons

UVA 1016 - Silly Sort(置换分解+贪心)

UVA 1016 - Silly Sort 题目链接 题意:给定一个序列,数字都不同,每次可以交换两个数字,交换的代价为两数之和,要求出把这个序列变成递增最小代价 思路:利用置换的分解原理,可以把序列的每条循环单独考虑,对于每条循环而言,不断交换肯定每个数字至少会换到一次,再利用贪心的思想,如果每次拿循环中的最小值去置换,那么就是这个最小值会用长度-1次,而剩下的数字各一次,注意这里还有一种可能优的方法,就是先把整个序列中的最小值换到该循环中,等置换完再换出去,两种都考虑进来即可 代码: #in