哈理工oj 1756Merge Intervals【结构体 + 排序】

来源:互联网 发布:团队协作工具linux 编辑:程序博客网 时间:2024/05/17 05:51
Merge IntervalsTime Limit: 1000 MSMemory Limit: 65535 KTotal Submit: 65(30 users)Total Accepted: 32(28 users)Rating: Special Judge: NoDescription

Given a collection of intervals, merge all overlapping intervals.

For example,

Given: [1,3],[2,6],[8,10],[15,18];

after meger: [1,6],[8,10],[15,18].

Input

There are multiple test cases.

For each test case:

Line 1: This line contains an integer n indicating the number of intervals.

Line 2..n+1: Each line contains a pair of integers  a and b, indicating the interval [a, b].

1<=n<=100,000

1<=a <= b<=1,000,000,000

Output

Output one line, contains the intervals separated by space after the merge. Output the intervals in lexicographically smaller way.

In other words, if we output [ai,bi] before [aj,bj], there must be ai < aj or ai=aj and bi <= bj.

For more details, referring to the sample.

Sample Input
41 32 68 1015 18537 6529 4330 4231 8732 86
Sample Output
[1,6] [8,10] [15,18][29,87]
#include<bits/stdc++.h>using namespace std;struct node{    int x, y;} uv[100005];bool cmp(node a, node b){    if(a.x != b.x)        return a.x < b.x;    return a.y < b.y;}int main(){    int n;    while(cin >> n)    {        for(int i = 0; i < n; i++)            cin >> uv[i].x >> uv[i].y;        sort(uv, uv + n, cmp);        int op = uv[0].x;        int ed = uv[0].y;        for(int i = 1; i < n; i++)        {            if(uv[i].x <= ed)            {                if(uv[i].y > ed)                    ed = uv[i].y;            }            else            {                printf("[%d,%d] ", op, ed);                op = uv[i].x;                ed = uv[i].y ;            }            if(i == n - 1)                printf("[%d,%d]\n", op, ed);        }    }    return 0;}