Intervals
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:5345 |
|
Accepted:2087 |
Description
There is given the series of n closed intervals [ai; bi], where I = 1, 2 ,..., n. the sum of those intervals may be represented as a sum of closed pairwise non −intersecting intervals. the task is to find such representation with the minimal number of intervals. the intervals of this representation shocould be written in the output file in acceding order. we say that the intervals [a; B] and [c; d] are in ascending order if, and only if a <= B <c <= d.
Task
Write a program which:
Reads from the std input the description of the series of intervals,
Computes pairwise non − intersecting intervals satisfying the conditions given abve,
Writes the computed intervals in ascending order into std output
Input
In the first line of input there is one integer n, 3 <=n <= 50000. this is the number of intervals. in the (I + 1) −st line, 1 <= I <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval, 1 <= ai <= bi <= 1000000.
Output
The output shoshould contain descriptions of all computed pairwise non-intersecting intervals. in each line shoshould be written a description of one interval. it shoshould be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. the intervals shoshould be written into the output in ascending order.
Sample Input
55 61 410 106 98 10
Sample Output
1 45 10
Returns the Union of multiple subsets.
Relatively watery questions, it is easy to think of sorting the left interval of each subset, and then merging adjacent two sets .... I did not pay too much attention to the details, resulting in WA once...
#include<iostream>#include<algorithm>using namespace std;int n;__int64 p[50010],t;int main(){int i,x,y;while(cin>>n&&n){for(i=0;i<n;i++){scanf("%I64d",&t);p[i]=t*10000000;scanf("%I64d",&t);p[i]+=t;}sort(p,p+n);x=p[0]/10000000;y=p[0]%10000000;for(i=1;i<n;i++){if(y<p[i]/10000000){cout<<x<<' '<<y<<endl;x=p[i]/10000000;y=p[i]%10000000;}else if(y<p[i]%10000000)y=p[i]%10000000;}cout<<x<<' '<<y<<endl;}return 0;}