Intervals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8276   Accepted: 3270

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

 
题意:合并N个区间(有交即合并)
代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
} int N;
struct data{
int fir,sec;
}a[50001],ans[50001]; bool cmp(data a,data b){
if(a.fir!=b.fir) return a.fir<b.fir;
return a.sec<b.sec;
} int main(){
N=read();
for(int i=1;i<=N;i++){
a[i].fir=read();
a[i].sec=read();
}
sort(a+1,a+N+1,cmp);
int str=a[1].fir,end=a[1].sec;
int tmp=0;
for(int i=2;i<=N;i++){
if(a[i].fir>end){
ans[++tmp].fir=str;
ans[tmp].sec=end;
str=a[i].fir;
end=a[i].sec;
continue;
}
else end=max(end,a[i].sec);
}
ans[++tmp].fir=str;
ans[tmp].sec=end;
for(int i=1;i<=tmp;i++){
printf("%d %d\n",ans[i].fir,ans[i].sec);
}
}

  

【POJ】1089Intervals的更多相关文章

  1. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  4. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  5. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  6. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. nyoj 15 括号匹配(二) (经典dp)

    题目链接 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些 ...

  2. Commonjs,AMD,CMD和UMD的差异

    CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...

  3. 对象方法、类方法、原型方法 && 私有属性、公有属性、公有静态属性

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. mysql where/having区别

    mysql> select 2-1 as a,password from mysql.user where user='root' having a>0; +---+----------- ...

  5. Python模块学习 - Argparse

    argparse模块 在Python中,argparse模块是标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块.argparse模块能够根据程序中的定义从sys.argv中解析 ...

  6. vim查找/替换字符串【转】

    转自:http://www.cnblogs.com/GODYCA/archive/2013/02/22/2922840.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细 ...

  7. Microsoft Security Essential: 微软安全软件

    Microsoft Security Essential: 微软安全软件 这个杀毒软件终身免费

  8. centos_7.1.1503_src_7

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ tex-fonts-hebrew-0.1-21.el7.src.rpm 05-Jul-201 ...

  9. EF添加ADO.NET实体模型处直接选择Oracle数据源

    上一文介绍了如何下载Mysql for vs Tools来进行Mysql的ADO.NET实体模型数据源选择,今天将Oracle的测试了下.步骤如下: 1.在你项目Model层中nuget安装选中项 2 ...

  10. 相册框架之AssetsLibrary

    看到项目中用到AssetsLibrary这个框架, 所以研究了一下 其实很简单 - (void)getAlbumPhoto { NSString *tipTextWhenNoPhotosAuthori ...