Minimum Average Waiting Time

Problem Statement

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes.

Different kinds of pizzas take different amounts of time to cook. Also, once he starts cooking a pizza, he cannot cook another pizza until the first pizza is completely cooked. Let's say we have three customers who come at time t=0, t=1, & t=2 respectively, and the time needed to cook their pizzas is 3, 9, & 6 respectively. If Tieu applies first-come, first-served rule, then the waiting time of three customers is 3, 11, & 16 respectively. The average waiting time in this case is (3 + 11 + 16) / 3 = 10. This is not an optimized solution. After serving the first customer at time t=3, Tieu can choose to serve the third customer. In that case, the waiting time will be 3, 7, & 17 respectively. Hence the average waiting time is (3 + 7 + 17) / 3 = 9.

Help Tieu achieve the minimum average waiting time. For the sake of simplicity, just find the integer part of the minimum average waiting time.

Input Format

  • The first line contains an integer N, which is the number of customers.
  • In the next N lines, the ith line contains two space separated numbers Ti and Li. Ti is the time when ith customer order a pizza, and Li is the time required to cook that pizza.

Output Format

  • Display the integer part of the minimum average waiting time.

Constraints

  • 1 ≤ N ≤ 105
  • 0 ≤ Ti ≤ 109
  • 1 ≤ Li ≤ 109

Note

  • The waiting time is calculated as the difference between the time a customer orders pizza (the time at which they enter the shop) and the time she is served.

  • Cook does not know about the future orders.

Sample Input #00

3
0 3
1 9
2 6

Sample Output #00

9

Sample Input #01

3
0 3
1 9
2 5

Sample Output #01

8

Explanation #01

Let's call the person ordering at time = 0 as A, time = 1 as B and time = 2 as C. By delivering pizza for AC and B we get the minimum average wait time to be

(3 + 6 + 16)/3 = 25/3 = 8.33 

the integer part is 8 and hence the answer.

这道题我看了两个小时,十分简单的一道题,原因是我把题意给理解错了。我原先的理解是根据这怎样安排这N个顾客的服务顺序使得平均等待时间最短。其实问题不是这样的,问题是在当前时间t内,对所有已经到达的顾客中选择一个进行服务,最终使得平均等待时间最短,Heap的应用。

不能预知未来的程序:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
typedef long long ll;

inline ll max(ll a, ll b) {
    return a > b ? a : b;
}

struct Node1 {
    ll a, b;
    Node1() {}
    Node1(ll t_a, ll t_b) : a(t_a), b(t_b) {}
    bool operator < (const Node1 &p) const {
        return a < p.a;
    }
}node[maxn];

struct Node2 {
    ll a, b;
    Node2() {}
    Node2(ll t_a, ll t_b) : a(t_a), b(t_b) { }
    bool operator < (const Node2 &p) const {
        return b > p.b;
    }
};


int main() {

    //freopen("aa.in", "r", stdin);

    int n;
    ll t = 0;
    ll ans = 0;
    int id = 1;
    Node2 p;
    priority_queue<Node2> pq;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%lld %lld", &node[i].a, &node[i].b);
        ans -= node[i].a;
    }
    sort(node, node + n);
    t = node[0].a;
    pq.push(Node2(node[0].a, node[0].b));
    while(!pq.empty() || id < n) {
        while(id < n) {
            if(node[id].a <= t) {
                pq.push(Node2(node[id].a, node[id].b));
                id++;
            } else {
                break;
            }
        }
        p = pq.top();
        pq.pop();
        t = t + p.b;
        ans += t;
    }
    printf("%lld\n", ans / n);
    return 0;
}

能够预知未来的程序:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
typedef long long ll;

struct Node1 {
    ll a, b;
    Node1() {}
    Node1(ll t_a, ll t_b) : a(t_a), b(t_b) {}
    bool operator < (const Node1 &p) const {
        return a + b > p.a + p.b;
    }
};

struct Node2 {
    ll a, b;
    Node2() {}
    Node2(ll t_a, ll t_b) : a(t_a), b(t_b) {}
    bool operator < (const Node2 &p) const {
        return b > p.b;
    }
};

inline ll max(ll a, ll b) {
    return a > b ? a : b;
}

int main() {

    priority_queue<Node1> pq1;
    priority_queue<Node2> pq2;
    int n;
    ll a, b;
    ll t = 0;
    ll ans = 0;
    Node1 p1;
    Node2 p2;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%lld %lld", &a, &b);
        ans -= a;
        pq1.push(Node1(a, b));
    }
    while(!pq1.empty() || !pq2.empty()) {
        if(!pq1.empty()) {
            if(pq1.top().a < t) {
                while(!pq1.empty() && pq1.top().a < t) {
                    p1 = pq1.top();
                    pq1.pop();
                    p2.a = p1.a;
                    p2.b = p1.b;
                    pq2.push(p2);
                }
                if(!pq1.empty()) {
                    p1 = pq1.top();
                    p2 = pq2.top();
                    if(p1.a + p1.b <= t + p2.b) {
                        if(p1.a + p1.b == t + p2.b && p1.b < p2.b) {
                            t = t + p2.b;
                            pq2.pop();
                        } else {
                            t = p1.a + p1.b;
                            pq1.pop();
                        }
                    } else {
                        t = t + p2.b;
                        pq2.pop();
                    }
                } else {
                    p2 = pq2.top();
                    pq2.pop();
                    t = t + p2.b;
                }
            } else {
                p1 = pq1.top();
                if(!pq2.empty()) p2 = pq2.top();
                if(pq2.empty() || p1.a + p1.b <= t + p2.b) {
                    if(!pq2.empty() && p1.a + p1.b == t + p2.b && p1.b < p2.b) {
                        t = t + p2.b;
                        pq2.pop();
                    } else {
                        pq1.pop();
                        t = p1.a + p1.b;
                    }
                } else {
                    t = t + p2.b;
                    pq2.pop();
                }
            }
            ans += t;
            continue;
        }
        if(!pq2.empty()) {
            p2 = pq2.top();
            pq2.pop();
            t = t + p2.b;
            ans += t;
        }
    }
    printf("%lld\n", ans / n);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值