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

 
 
正解:贪心+排序
解题报告:
  水题。区间覆盖,排一遍序即可。
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int n; struct seq{
int l,r;
}a[MAXN]; inline bool cmp(seq q,seq qq){ if(q.l==qq.l) return q.r<qq.r; return q.l<qq.l; } inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void solve(){
n=getint();
for(int i=;i<=n;i++) {
a[i].l=getint(); a[i].r=getint();
}
sort(a+,a+n+,cmp);
int nowl=a[].l,nowr=a[].r;
for(int i=;i<=n;i++) {
if(a[i].l>nowr) {
printf("%d %d\n",nowl,nowr);
nowl=a[i].l; nowr=a[i].r;
}
else if(a[i].r>nowr) nowr=a[i].r;
} printf("%d %d",nowl,nowr);
} int main()
{
solve();
return ;
}

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. Java Executor并发框架(二)剖析ThreadPoolExecutor运行过程

    上一篇从整体上介绍了Executor接口,从上一篇我们知道了Executor框架的最顶层实现是ThreadPoolExecutor类,Executors工厂类中提供的newScheduledThrea ...

  2. MySchool

    USE [MySchool] GO /****** Object: Table [dbo].[Grade] Script Date: 08/06/2014 15:03:17 ******/ SET A ...

  3. vue中如何不通过路由直接获取url中的参数

    前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...

  4. 爆炸吧 js dom ---------> boom

    dom-> html css 事件 元素 eventlistener HTML 事件的例子: 当用户点击鼠标时 当网页已加载时 当图像已加载时 当鼠标移动到元素上时 当输入字段被改变时 当提交 ...

  5. mysql视图的创建

    视图内容的变化跟它所依赖的表的变化是同步的也是一致的. create or replace view viewname as select a.id.,a.name,a.sex,b.aid,b.sco ...

  6. linux运维中的命令梳理(一)

    在linux日常运维中,我们平时会用到很多常规的操作命令. 下面对常用命令进行梳理: 命令行日常系快捷键(不分大小写)CTRL + A 移动光标到行首CTRL + E 移动光标到行末CTRL + U ...

  7. 图片button

  8. 【转】【C#】ColorMatrix

    ColorMatrix(色彩矩阵),是GDI+里用来调整图片色彩的矩阵. 什么是矩阵,说白了就是C#里的二维数组. 那么这个矩阵调整色彩的原理是什么,他是怎么来调整色彩的呢?这个要从线性代数里的矩阵相 ...

  9. WorldWind源码剖析系列:WorldWind实时确定、更新、初始化和渲染地形和纹理数据

    WorldWind实时确定.更新.初始化和渲染地形和纹理数据 当用户点击WorldWind中的地球时,首先响应的是WorldWindow.OnPaint()函数,后续程序的调用流程如下图所示. 零散知 ...

  10. Objective-c复制对象的概念