Educational Codeforces Round 37 B. Tea Queue 模拟

B. Tea Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently n students from city S moved to city P to attend a programming camp.

They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.

i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea.

For each student determine the second he will use the teapot and get his tea (if he actually gets it).

Input

The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000).

Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students.

Then n lines follow. Each line contains two integer liri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea.

It is guaranteed that for every  condition li - 1 ≤ li holds.

The sum of n over all test cases doesn't exceed 1000.

Note that in hacks you have to set t = 1.

Output

For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.

Example
input
Copy
2
2
1 3
1 4
3
1 5
1 1
2 3
output
1 2 
1 0 2 
Note

The example contains 2 tests:

  1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second.
  2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea.

题意:有一队学生排队接茶,每个人都有进入队的时间与在队中等待的时间,如果两人同时入队则编号小的人排在前面。求每个人是否接到茶,以及接到茶的时间。

思路:构造一个队列,按时间遍历,在对应时间将相应学生加入队列,并判断其是否已经超出等待时间。

代码如下:

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int id,l;
}stu[1100];

int R[1100],n,ans[1100];

bool cmp(node x,node y)
{
    if (x.l==y.l)
        return x.id<y.id;
    return x.l<y.l;
}

int main()
{
    int _;
    scanf("%d",&_);
    while (_--){
        memset(ans,0,sizeof(ans));
        int tmax=0;
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
            scanf("%d %d",&stu[i].l,&R[i]);
            stu[i].id=i;
            tmax=max(tmax,R[i]);
        }
        sort(stu+1,stu+n+1,cmp);
        queue<node> q;
        int pos=1;
        for (int time=1;time<=tmax;time++){
            while (pos<=n&&stu[pos].l==time){
                q.push(stu[pos++]);
            }
            while (!q.empty()){
                if (R[q.front().id]<time)       //在队中超出了等待时间还没有接到茶
                    q.pop();
                else
                    break;
            }
            if (!q.empty()){
                ans[q.front().id]=time;         //接到茶
                q.pop();
            }
        }
        for (int i=1;i<=n;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值