UVa 1153 Keep the Customer Satisfied 解题报告(贪心)

本文介绍了一种确保客户满意度最大化的算法,通过合理安排订单生产计划来最小化拒绝订单的数量,详细解释了问题背景、输入输出要求及示例,并给出了实现思路与代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1153 - Keep the Customer Satisfied

Time limit: 3.000 seconds

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, the amount of steel required (in tons) and d, 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 tons of steel takes exactly q 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. For a given order, Jj, qj denotes the amount of steel required and dj is the associated due date.

Orderqjdj
J168
J249
J3715
J4820
J5321
J6522

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 and J4, accept all other orders and process them as follows.

Accepted OrderStarting TimeCompletion Time
J204
J3411
J51114
J61419

Note that the production line is never idle.

Input 

The input begins with a single positive integer on a line by itself indicatingthe 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 betweentwo 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 of orders (n can be as large as 800000 for some test cases). It is followed by n 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).

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 and Jv with qu > qv and du < dv, if Ju is accepted then Jv is also accepted.
  • Finally, Hogdson and Moore advise you to ``Keep the Customer Satisfied"

    解题报告: 其实下面的提示就基本告诉我们怎么做这题了。首先按照截止时间的先后排序。对于任意两个任务a和b,如果a的截止时间在b之前,且a的加工时间比b长,那么接受了a订单必然要接受b订单。反过来呢,如果b的加工时间超过了截止时间,那么就找之前的订单,删掉加工时间最长的那个订单。这样接受的订单数没有变化,而总的加工时间变短了,为以后接受更多订单做准备。

    使用优先队列维护以及接受的订单。代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM

    work();
}

/****************************************/

struct Node
{
    int q, d;
    bool operator<(const Node & cmp) const
    {
        return d == cmp.d ? q < cmp.q : d < cmp.d;
    }
} node[800010];

void work()
{
    int T;
    scanf("%d", &T);
    fff(cas, 1, T)
    {
        int n;
        scanf("%d", &n);

        ff(i, n) scanf("%d%d", &node[i].q, &node[i].d);
        sort(node, node+n);

        int ans = 0;
        int cur = 0;

        priority_queue<int> que;
        ff(i, n)
        {
            if(cur <= node[i].d - node[i].q)
            {
                ans++;
                cur += node[i].q;
                que.push(node[i].q);
            }
            else if(que.size() && que.top() > node[i].q)
            {
                cur -= que.top();
                cur += node[i].q;
                que.pop();
                que.push(node[i].q);
            }
        }

        printf("%d\n", ans);

        if(cas != T) puts("");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值