您的位置:首页 > 大数据 > 人工智能

Minimum Average Waiting Time

2015-05-29 12:02 363 查看
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
[code]3
0 3
1 9
2 6


Sample Output
#00
[code]9


Sample Input
#01
[code]3
0 3
1 9
2 5


Sample Output
#01
[code]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 A, C and B we
get the minimum average wait time to be
[code](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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: