Intervals

Description

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.

Input

The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.

Output

The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.

Sample Input

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Output

6

Source

 
  1 #include <stdlib.h>
  2 
  3 #include <iostream>
  4 
  5 #include <stdio.h>
  6 
  7 #include <cstring>
  8 
  9 #include <algorithm>
 10 
 11 
 12 using namespace std;
 13 
 14 
 15 struct edge {
 16 
 17     int to, value, next;
 18 
 19 
 20 } e[200100];
 21 
 22 int que[200100], last, first;
 23 
 24 bool in[200100];
 25 
 26 int ct = 0;
 27 
 28 int lin[50020], dis[50020];
 29 
 30 int n;
 31 
 32 #define inf -99999999
 33 
 34 int lisan[50020], list[50020];
 35 
 36 
 37 /*
 38 
 39  * 
 40 
 41  */
 42 
 43 int main(int argc, char** argv) {
 44 
 45     int m, i, j, k, l, a, b, c;
 46 
 47     scanf("%d", &m);
 48 
 49     memset(lin, 0, sizeof (lin));
 50 
 51     memset(lisan, 0, sizeof (lisan));
 52 
 53 
 54     ct = 0;
 55 
 56     n = 0;
 57 
 58     while (m--) {
 59 
 60 
 61         scanf("%d %d %d", &a, &b, &c);
 62 
 63 
 64         b++;
 65 
 66         if (lisan[a] == 0) {
 67 
 68             n++;
 69 
 70             lisan[a] = n;
 71 
 72             list[n] = a;
 73 
 74  
 75 
 76         }
 77 
 78         if (lisan[b] == 0) {
 79 
 80             n++;
 81 
 82             lisan[b] = n;
 83 
 84             list[n] = b;
 85 
 86 
 87         }
 88 
 89         a = lisan[a];
 90 
 91         b = lisan[b];
 92 
 93  
 94 
 95         ct++;
 96 
 97         e[ct].value = c;
 98 
 99         e[ct].to = b;
100 
101         e[ct].next = lin[a];
102 
103         lin[a] = ct;
104 
105  
106 
107     }
108 
109     sort(list + 1, list + 1 + n);
110 
111     for (i = 1; i < n; i++) {
112 
113         dis[i ] = inf;
114 
115         ct++;
116 
117         e[ct].value = 0;
118 
119         e[ct].to = lisan[list[i + 1 ]];
120 
121         e[ct].next = lin[lisan[list[i ]]];
122 
123         lin[lisan[list[i ]]] = ct;
124 
125         ct++;
126 
127         e[ct].value = -(list[i + 1] - list[i]);
128 
129         e[ct].to = lisan[list[i ]];
130 
131         e[ct].next = lin[lisan[list[i + 1]]];
132 
133         lin[lisan[list[i + 1 ]]] = ct;
134 
135 
136     }
137 
138     dis[n] = inf;
139 
140     dis[lisan[list[1]]] = 0;
141 
142     first = 0;
143 
144     last = 1;
145 
146     memset(in, 0, sizeof (in));
147 
148     in[lisan[list[1]]] = 1;
149 
150     void bell();
151 
152     que[0] = lisan[list[1]];
153 
154     bell();
155 
156     printf("%d\n", dis[lisan[list[n]]]);
157 
158 
159     return (EXIT_SUCCESS);
160 
161 }
162 
163 
164 void bell() {
165 
166  
167 
168     int i, j, k, l;
169 
170 
171     while (first != last) {
172 
173 
174         i = que[first];
175 
176         in[i] = 0;
177 
178         first++;
179 
180         if (first > 200010)
181 
182             first = 0;
183 
184         j = lin[i];
185 
186         while (j) {
187 
188             k = e[j].to;
189 
190             if (dis[i] + e[j].value > dis[k]) {
191 
192                 dis[k] = dis[i] + e[j].value;
193 
194                 if (!in[k]) {
195 
196                     que[last] = k;
197 
198                     in[k] = 1;
199 
200                     last++;
201 
202                     if (last > 200010)
203 
204                         last = 0;
205 
206                 }
207 
208 
209             }
210 
211 
212             j = e[j].next;
213 
214         }
215 
216  
217 
218     }
219 
220  
221 
222 }
223 
224  

 

posted @ 2012-05-30 21:50  cseriscser  阅读(363)  评论(0编辑  收藏  举报