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
  1. 3
    9 2
    7 3
    2 4
Output
  1. 9 10
    1 3
    4 7
Input
  1. 4
    1000000000 1000000
    1000000000 1000000
    100000000 1000000
    1000000000 1000000
Output
  1. 1000000000 1000999999
    1 1000000
    100000000 100999999
    1000001 2000000
  2.  
  3. 题意:n个要求 给出期望起始时间以及持续时间 若期望的时间段空闲 则安排当前要求 在这个时间段 否则从1开始向后寻找一个合适的
    空闲时间段 安排当前要求。输出n个要求的 时间段。
    题解:可能写复杂了,优先队列处理。
  1. /******************************
  2. code by drizzle
  3. blog: www.cnblogs.com/hsd-/
  4. ^ ^ ^ ^
  5. O O
  6. ******************************/
  7. #include<bits/stdc++.h>
  8. #include<map>
  9. #include<set>
  10. #include<cmath>
  11. #include<queue>
  12. #include<bitset>
  13. #include<math.h>
  14. #include<vector>
  15. #include<string>
  16. #include<stdio.h>
  17. #include<cstring>
  18. #include<iostream>
  19. #include<algorithm>
  20. #pragma comment(linker, "/STACK:102400000,102400000")
  21. using namespace std;
  22. #define A first
  23. #define B second
  24. const int mod=;
  25. const int MOD1=;
  26. const int MOD2=;
  27. const double EPS=0.00000001;
  28. typedef __int64 ll;
  29. const ll MOD=;
  30. const int INF=;
  31. const ll MAX=1ll<<;
  32. const double eps=1e-;
  33. const double inf=~0u>>;
  34. const double pi=acos(-1.0);
  35. typedef double db;
  36. typedef unsigned int uint;
  37. typedef unsigned long long ull;
  38. int n;
  39. ll s[];
  40. ll d[];
  41. struct node
  42. {
  43.  
  44. ll l,r;
  45. friend bool operator < (node a, node b)
  46. {
  47. return a.l > b.l;
  48. }
  49. } now;
  50. priority_queue<node> pq;
  51. queue<node> exm;
  52. int main()
  53. {
  54. scanf("%d",&n);
  55. for(int i=; i<=n; i++)
  56. {
  57. scanf("%I64d %I64d",&s[i],&d[i]);
  58. d[i]=s[i]+d[i]-;
  59. }
  60. now.l=;
  61. now.r=s[]-;
  62. if(now.l<=now.r)
  63. pq.push(now);
  64. now.l=d[]+;
  65. now.r=;
  66. pq.push(now);
  67. for(int i=; i<=n; i++)
  68. {
  69. int flag=;
  70. while(!exm.empty())
  71. {
  72. now=exm.front();
  73. pq.push(now);
  74. exm.pop();
  75. }
  76. while(!pq.empty())
  77. {
  78. now=pq.top();
  79. pq.pop();
  80. if(d[i]<=now.r&&s[i]>=now.l)
  81. {
  82. ll gg=now.r;
  83. now.r=s[i]-;
  84. if(now.l<=now.r)
  85. pq.push(now);
  86. now.r=gg;
  87. now.l=d[i]+;
  88. if(now.l<=now.r)
  89. pq.push(now);
  90. flag=;
  91. break;
  92. }
  93. else
  94. {
  95. exm.push(now);
  96. }
  97. }
  98. while(!exm.empty())
  99. {
  100. now=exm.front();
  101. pq.push(now);
  102. exm.pop();
  103. }
  104. if(flag==)
  105. {
  106. while(!pq.empty())
  107. {
  108. now=pq.top();
  109. pq.pop();
  110. if((d[i]-s[i])<=(now.r-now.l))
  111. {
  112. d[i]=now.l+d[i]-s[i];
  113. s[i]=now.l;
  114. if(d[i]<now.r)
  115. now.l=d[i]+;
  116. pq.push(now);
  117. break;
  118. }
  119. else
  120. {
  121. exm.push(now);
  122. }
  123. }
  124. }
  125. }
  126. for(int i=; i<=n; i++)
  127. printf("%I64d %I64d\n",s[i],d[i]);
  128. return ;
  129. }

2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) G 优先队列的更多相关文章

  1. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution

    从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...

  2. Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结

    第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...

  3. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

  4. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A. Find a Number 找到一个树,可以被d整除,且数字和为s 记忆化搜索 static class S{ int mod,s; String str; public S(int mod, ...

  5. 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)

    i207M的“怕不是一个小时就要弃疗的flag”并没有生效,这次居然写到了最后,好评=.= 然而可能是退役前和i207M的最后一场比赛了TAT 不过打得真的好爽啊QAQ 最终结果: 看见那几个罚时没, ...

  6. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) Solution

    A. Find a Number Solved By 2017212212083 题意:$找一个最小的n使得n % d == 0 并且 n 的每一位数字加起来之和为s$ 思路: 定义一个二元组$< ...

  7. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  8. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  9. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror) in codeforces(codeforces730)

    A.Toda 2 思路:可以有二分来得到最后的数值,然后每次排序去掉最大的两个,或者3个(奇数时). /************************************************ ...

  10. 【*2000】【2018-2019 ICPC, NEERC, Southern Subregional Contest C 】Cloud Computing

    [链接] 我是链接,点我呀:) [题意] [题解] 我们可以很容易知道区间的每个位置有哪些安排可以用. 显然 我们优先用那些花费的钱比较少的租用cpu方案. 但一个方案可供租用的cpu有限. 我们可以 ...

随机推荐

  1. FOJ 2105 Digits Count

    题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1  直接无视,是0则成段赋值为0: or  如 ...

  2. Hibernate中的继承映射

    1.继承映射 继承映射分为两种情况:简单继承映射和继承映射. 在简单继承映射中,每个子类都要写一个映射文件. 在继承映射中,只使用一个映射文件.继承映射分为三种情况: 所有子类映射到一张表 需要使用鉴 ...

  3. Autolayout-VFL语言添加约束

    一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...

  4. 修改Azure Website 移动服务 默认时区

    Azure Website 默认时区为国际标准时间,对中国用户来说不太方便友好,如何设置成北京时间呢? 打开Azure Website的“配置”页,找到“应用设置”节点. 在应用设置中添加设置项,密钥 ...

  5. iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020)

    看看IIS的网站,惊人的发现default web site是停止状态.印象中没有停止它啊.右键->管理网站->启动.点击启动后居然弹出:“另一个程序正在使用此文件,进程无法访问.(异常来 ...

  6. HD OJ2023

    #include "stdio.h"double stu[60],cla[10];int a[60][60];int main(){ int n,m,i,number,j; whi ...

  7. device framework(设备框架)

    Table A-1  Device frameworks Name First available Prefixes Description Accelerate.framework 4.0 cbla ...

  8. Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]

    在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一. ...

  9. BZOJ 1600 建造栅栏

    O(N)分成1,2与3,4两部分搞一搞. #include<iostream> #include<cstdio> #include<cstring> #includ ...

  10. Logger日志打印普通方法

    using System; using System.IO; using System.Text; namespace Core { public class LogHelper { private ...