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. 取(m堆)石子游戏 HDU2176(Nim博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题目: Problem Description m堆石子,两人轮流取.只能在1堆中取.取完者胜. ...

  2. DIV+CSS左右列高度自适应问题

    其实解决DIV+CSS左右两列高度自适应的方法就是要注意两点:一是在最外层加上overflow:hidden,然后在左边列加上margin-bottom:-9999px;padding-bottom: ...

  3. webpack版本1与版本2的若干写法区别

    2.x的环境遇到类似this._init is not a function的报错. 版本1.x的写法: resolve: { extensions: ['', '.js', '.vue'] }, m ...

  4. yocto 离线编译

    使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...

  5. linux percpu机制解析【转】

    转自:http://blog.csdn.net/wh8_2011/article/details/53138377 一.概述 每cpu变量是最简单也是最重要的同步技术.每cpu变量主要是数据结构数组, ...

  6. Ubuntu 14.04 安装gstreamer0.10-ffmpeg

    sudo apt-add-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install -y gstreame ...

  7. UVALive 7040 Color

    题目链接:LA-7040 题意为用m种颜色给n个格子染色.问正好使用k种颜色的方案有多少. 首先很容易想到的是\( k * (k-1)^{n-1}\),这个算出来的是使用小于等于k种颜色给n个方格染色 ...

  8. 尽量用const,enum,inline代替define

    在读<Effective C++>之前,我确实不知道const,enum,inline会和define扯上什么关系,看完感觉收获很大,记录之. define: 宏定义. 在编译预处理时,对 ...

  9. LCT 文档

    file:///C:/Users/Frank/Downloads/QTREE%E8%A7%A3%E6%B3%95%E7%9A%84%E4%B8%80%E4%BA%9B%E7%A0%94%E7%A9%B ...

  10. tomcat修改内存

    windows: 修改bin/catalina.bat, 第一行添加 set JAVA_OPTS=-Xms256m -Xmx512m linux: 修改bin/catalina.sh 第一行添加 JA ...