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
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <list>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
const double PI = acos(-1.0);
const int maxn = ;
const int INF = 0x3f3f3f3f;
int dx[]={,,-,};
int dy[]={-,,,}; int n,m;
int f[maxn],cnt=,sum=;
struct node
{
int l,r;
}a[maxn];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
scanf("%d %d",&a[i].l,&a[i].r);
sort(a,a+n,cmp);
int x=a[].l,y=a[].r;
for(int i=;i<n;i++)
{
if(a[i].l>=x && a[i].l<=y)
{
if(a[i].r>y)
y=a[i].r;
}
else
{
printf("%d %d\n",x,y);
x=a[i].l;
y=a[i].r;
}
}
printf("%d %d\n",x,y);
}
}
/* */

POJ 1089 Intervals【合并n个区间/贪心】的更多相关文章

  1. poj 1089 Intervals

    http://poj.org/problem?id=1089 Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  2. Intervals ZOJ - 3953 (区间贪心)

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  3. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  4. [LeetCode] 56 - Merge Intervals 合并区间

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

  5. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  6. HDU 1936 区间贪心

    /* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...

  7. 力扣leetcode 435. 无重叠区间 - 贪心

    非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...

  8. TZOJ 4007 The Siruseri Sports Stadium(区间贪心)

    描述 The bustling town of Siruseri has just one sports stadium. There are a number of schools, college ...

  9. HDU 2037 今年暑假不AC (区间贪心)

    题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...

随机推荐

  1. Lucene6.6添加索引数据时字符个数超限,字符数不能超过BYTE_BLOCK_SIZE=32766

    最近发现Lucene6.6版本添加索引数据字符数超过32766时,出现报错,而Lucene4.6版本中则未出现这一问题,原因如下: 概述:         添加索引数据时,对于分词字段,分词后的Ter ...

  2. 解决SpringSecurity限制iframe引用页面的问题

    使用Spring Security的过程中,需要使用iframe来引入其他域的页面,页面会报X-Frame-Options的错误,试了好几种方法一直未能很好的解决这个问题. 这里涉及到Spring S ...

  3. HDU 2084 数塔 (dp)

    题目链接 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数 ...

  4. JVM在遇到OOM(OutOfMemoryError)时生成Dump文件

    方法一: 命令:jmap -dump:format=b,file=heap.bin file:保存路径及文件名pid:进程编号(windows通过任务管理器查看,linux通过ps aux查看) du ...

  5. shell下在while循环中使用ssh命令的问题

    1 现象描述 最近使用ssh批量执行命令(已经做了密钥互信了),脚本读取配置文件中的主机列表(内容为每行一台主机IP地址),然后执行,可是每次只是执行第一台,就退出循环了. 2 排查思路 由于脚本比较 ...

  6. python基础===jieba模块,Python 中文分词组件

    api参考地址:https://github.com/fxsjy/jieba/blob/master/README.md 安装自行百度 基本用法: import jieba #全模式 word = j ...

  7. 64_j1

    JSCookMenu-2.0.4-13.fc26.noarch.rpm 13-Feb-2017 22:06 38098 Java-WebSocket-1.3.1-0.2.git58d1778.fc24 ...

  8. XMLDocument 方法中实现post发送消息

    XMLDocument 方法中实现post发送消息

  9. IE11中实现颜色渐变

    background: -ms-linear-gradient(left,#daa23e,#ad7f27); 下面是css3中颜色渐变对各个浏览器的写法:background: -webkit-lin ...

  10. jdbc预编译插入数据操作

    package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...