POJ1089 Intervals

 

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 should 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 above, 
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 should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.

Sample Input

5
5 6
1 4
10 10
6 9
8 10

Sample Output

1 4
5 10

Source

 
 
正解:贪心+排序
解题报告:
  水题。区间覆盖,排一遍序即可。
 
 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #ifdef WIN32   
13 #define OT "%I64d"
14 #else
15 #define OT "%lld"
16 #endif
17 using namespace std;
18 typedef long long LL;
19 const int MAXN = 50011;
20 int n;
21 
22 struct seq{
23     int l,r;
24 }a[MAXN];
25 
26 inline bool cmp(seq q,seq qq){ if(q.l==qq.l) return q.r<qq.r; return q.l<qq.l; }
27 
28 inline int getint()
29 {
30        int w=0,q=0;
31        char c=getchar();
32        while((c<'0' || c>'9') && c!='-') c=getchar();
33        if (c=='-')  q=1, c=getchar();
34        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
35        return q ? -w : w;
36 }
37 
38 
39 inline void solve(){
40     n=getint();
41     for(int i=1;i<=n;i++) {
42     a[i].l=getint(); a[i].r=getint();
43     }
44     sort(a+1,a+n+1,cmp);    
45     int nowl=a[1].l,nowr=a[1].r;
46     for(int i=2;i<=n;i++) {
47     if(a[i].l>nowr) {
48         printf("%d %d\n",nowl,nowr);
49         nowl=a[i].l; nowr=a[i].r;
50     }
51     else if(a[i].r>nowr)  nowr=a[i].r;    
52     }
53 
54     printf("%d %d",nowl,nowr);
55 }
56 
57 int main()
58 {
59   solve();
60   return 0;
61 }

 

转载于:https://www.cnblogs.com/ljh2000-jump/p/5605087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值