银行排队模拟

来源:互联网 发布:淘宝卖家批量发货 编辑:程序博客网 时间:2024/04/30 04:32
Baggage Room
Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice SGU 363

Description



Schoolboys Vasya and Pete travel a lot. They use baggage rooms really often. Baggage room has M windows, numbered 1 through M. When a lot of people come, there are big queues in front of windows. Newly come person stands in the queue with the least number of people. If there are several such queues than he or she chooses queue to the window with lowest number. When the queue is chosen, the decision will not further change. When next person comes to the window, he or she spends some time near the window to take or get his stuff, pay money, etc. (let's call it servicing time). This time is specific to each person and doesn't depend on the window. During this time the person is counted as standing in the queue. If new person come in the moment when one or several peoplehave just proceeded to their windows, he or she waits first until these people will leave their queues and then he or she chooses the queue to stand in. Vasya and Pet e wonder when each particular person will leave the queue. Please help them.

Input

The first line of the input file contains two integer numbers N, M (1 ≤ N ≤ 100; 1 ≤ M ≤ 100), where N - number of people, M - number of windows in the baggage room. Next N lines contain pairs of integer numbers ki, ti (1 ≤ ki ≤ 1000; 1 ≤ ti ≤ 100), where ki - time moment, when ith person came, ti - servicing time of ith person. Persons are listed in order of increase of ki.

Output

Print N lines. Print in the ith line two numbers - number of the queue, that would be chosen by ith person and moment of the time, when he or she will leave the queue.

Sample Input

sample input
sample output
5 21 23 44 15 26 1
1 31 72 52 71 8

 

#include <iostream>#include <string.h>#include <cmath>#include <algorithm>using namespace std;#define MAXN 100 + 10int num[MAXN];int wait_time[MAXN];int current_time, last_time;int person[10 * MAXN];int time[10 * MAXN];bool vis[10 * MAXN];void input(){    int n, m;    while (cin >> n >> m)    {        memset(num, 0, sizeof(num));        memset(wait_time, 0, sizeof(wait_time));        memset(person, 0, sizeof(person));        memset(vis, false, sizeof(vis));        for (int i = 0; i < n; i++)        {            cin >> current_time >> last_time;            for (int j = 0; j < i; j++)            {                if (time[j] <= current_time && !vis[j])                {                    vis[j] = true;                    num[person[j]]--;                }            }            int *p = min_element(num, num + m);            person[i] = p - num;            num[p - num]++;            wait_time[p - num] = wait_time[p - num] > current_time ? wait_time[p - num] + last_time : current_time + last_time;            time[i] = wait_time[p - num];            cout << p - num + 1 << ' ' << time[i] << endl;        }    }}int main(){    input();    return 0;}


 

0 0
原创粉丝点击