2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) G 优先队列

时间:2021-10-24 13:27:11
G. Car Repair Shop
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.

Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came.

The i-th request is characterized by two values: si — the day when a client wants to start the repair of his car, di — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on.

Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows:

  • If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client.
  • Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si.

Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.

Input

The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients.

The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car.

The requests should be processed in the order they are given in the input.

Output

Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.

Examples
Input
3
9 2
7 3
2 4
Output
9 10
1 3
4 7
Input
4
1000000000 1000000
1000000000 1000000
100000000 1000000
1000000000 1000000
Output
1000000000 1000999999
1 1000000
100000000 100999999
1000001 2000000 题意:n个要求 给出期望起始时间以及持续时间 若期望的时间段空闲 则安排当前要求 在这个时间段 否则从1开始向后寻找一个合适的
空闲时间段 安排当前要求。输出n个要求的 时间段。
题解:可能写复杂了,优先队列处理。
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int n;
ll s[];
ll d[];
struct node
{ ll l,r;
friend bool operator < (node a, node b)
{
return a.l > b.l;
}
} now;
priority_queue<node> pq;
queue<node> exm;
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%I64d %I64d",&s[i],&d[i]);
d[i]=s[i]+d[i]-;
}
now.l=;
now.r=s[]-;
if(now.l<=now.r)
pq.push(now);
now.l=d[]+;
now.r=;
pq.push(now);
for(int i=; i<=n; i++)
{
int flag=;
while(!exm.empty())
{
now=exm.front();
pq.push(now);
exm.pop();
}
while(!pq.empty())
{
now=pq.top();
pq.pop();
if(d[i]<=now.r&&s[i]>=now.l)
{
ll gg=now.r;
now.r=s[i]-;
if(now.l<=now.r)
pq.push(now);
now.r=gg;
now.l=d[i]+;
if(now.l<=now.r)
pq.push(now);
flag=;
break;
}
else
{
exm.push(now);
}
}
while(!exm.empty())
{
now=exm.front();
pq.push(now);
exm.pop();
}
if(flag==)
{
while(!pq.empty())
{
now=pq.top();
pq.pop();
if((d[i]-s[i])<=(now.r-now.l))
{
d[i]=now.l+d[i]-s[i];
s[i]=now.l;
if(d[i]<now.r)
now.l=d[i]+;
pq.push(now);
break;
}
else
{
exm.push(now);
}
}
}
}
for(int i=; i<=n; i++)
printf("%I64d %I64d\n",s[i],d[i]);
return ;
}