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.
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.
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.
3
9 2
7 3
2 4
9 10
1 3
4 7
4
1000000000 1000000
1000000000 1000000
100000000 1000000
1000000000 1000000
1000000000 1000999999
1 1000000
100000000 100999999
1000001 2000000
题意: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=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 typedef __int64 ll; 29 const ll MOD=1000000007; 30 const int INF=1000000010; 31 const ll MAX=1ll<<55; 32 const double eps=1e-5; 33 const double inf=~0u>>1; 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[205]; 40 ll d[205]; 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=1; i<=n; i++) 56 { 57 scanf("%I64d %I64d",&s[i],&d[i]); 58 d[i]=s[i]+d[i]-1; 59 } 60 now.l=1; 61 now.r=s[1]-1; 62 if(now.l<=now.r) 63 pq.push(now); 64 now.l=d[1]+1; 65 now.r=10000000000; 66 pq.push(now); 67 for(int i=2; i<=n; i++) 68 { 69 int flag=0; 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]-1; 84 if(now.l<=now.r) 85 pq.push(now); 86 now.r=gg; 87 now.l=d[i]+1; 88 if(now.l<=now.r) 89 pq.push(now); 90 flag=1; 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==0) 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]+1; 116 pq.push(now); 117 break; 118 } 119 else 120 { 121 exm.push(now); 122 } 123 } 124 } 125 } 126 for(int i=1; i<=n; i++) 127 printf("%I64d %I64d\n",s[i],d[i]); 128 return 0; 129 }