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

  1. 5
  2. 5 6
  3. 1 4
  4. 10 10
  5. 6 9
  6. 8 10

Sample Output

  1. 1 4
  2. 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 = ;
  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=,q=;
  31. char c=getchar();
  32. while((c<'' || c>'') && c!='-') c=getchar();
  33. if (c=='-') q=, c=getchar();
  34. while (c>='' && c<='') w=w*+c-'', c=getchar();
  35. return q ? -w : w;
  36. }
  37.  
  38. inline void solve(){
  39. n=getint();
  40. for(int i=;i<=n;i++) {
  41. a[i].l=getint(); a[i].r=getint();
  42. }
  43. sort(a+,a+n+,cmp);
  44. int nowl=a[].l,nowr=a[].r;
  45. for(int i=;i<=n;i++) {
  46. if(a[i].l>nowr) {
  47. printf("%d %d\n",nowl,nowr);
  48. nowl=a[i].l; nowr=a[i].r;
  49. }
  50. else if(a[i].r>nowr) nowr=a[i].r;
  51. }
  52.  
  53. printf("%d %d",nowl,nowr);
  54. }
  55.  
  56. int main()
  57. {
  58. solve();
  59. return ;
  60. }

POJ1089 Intervals的更多相关文章

  1. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  2. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  4. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

  5. Understanding Binomial Confidence Intervals 二项分布的置信区间

    Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...

  6. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

    感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...

  8. Merge Intervals 运行比较快

    class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...

  9. [LeetCode] 435 Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

随机推荐

  1. android调试模拟器启动太慢,怎样才能更快的调试程序呢?

    答: 1. 模拟器不关,直接按调试按钮系统会自动重新安装软件的.

  2. iOS数组使用

    相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...

  3. Android应用开发中如何使用隐藏API(转)

    一开始需要说明的是,Google之所以要将一些API隐藏(指加上@hide标记的public类.方法或常量)是有原因的.其中很大的原因就是Android系统本身还在不断的进化发展中.从1.0.1.1到 ...

  4. 自定义WPF ListBox的选中项样式

    首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...

  5. [转]iptables详解

    FROM : http://blog.chinaunix.net/uid-26495963-id-3279216.html 一:前言   防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的 ...

  6. Linux Linux程序练习十一(网络编程大文件发送UDP版)

    //网络编程发送端--大文件传输(UDP) #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  7. no.1

    #import requests import urllib import bs4 try: html=urllib.urlopen('http://anyun.org') except HTTPer ...

  8. Enabling CORS in WCF

    Introduction This is an intermediate example of WCF as REST based solution and enabling CORS access, ...

  9. Linux及安全——模块

    Linux及安全——模块 一.模块的编译.生成.测试.删除 1.编写模块代码 编写:gedit test.c 查看:cat test.c 2.查看版本信息 3.编写Makefile obj-m :这个 ...

  10. 树莓派之web服务器搭建

    树莓派之web服务器搭建 (一)使用ufw创建防火墙 设置目的:可以完全阻止对树莓派的访问也可以用来配置通过防火墙对特点程序的访问.使用防火墙更好的保护树莓派. 准备工作 1.带有5V电源的树莓派 2 ...