Intervals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7214   Accepted: 2862

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
 
 
 

#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 50005 struct node {  int c,d; }f[N]; int cmp(const void*a,const void*b) {  if((*(struct node*)a).c==(*(struct node*)b).c)                //从区间左端从小到大排序   return (*(struct node*)a).d>(*(struct node*)b).d?1:-1;       //如果左端相等按右段排序  return (*(struct node*)a).c>(*(struct node*)b).c?1:-1; } int main() {     int n,i;  while(scanf("%d",&n)!=EOF)  {   for(i=0;i<n;i++)    scanf("%d%d",&f[i].c,&f[i].d);   qsort(f,n,sizeof(f[0]),cmp);   int a=f[0].c,b=f[0].d;   for(i=1;i<n;i++)   {    if(f[i].c>b)                //若区间不交叉,输出上一个区间    {     printf("%d %d\n",a,b);               a=f[i].c;     b=f[i].d;    }    else if(b<f[i].d)      b=f[i].d;        //否则,判断当前右端是否大于上一区间的右端   }   printf("%d %d\n",a,b);  }     return 0; }

poj 1089 Intervals的更多相关文章

  1. POJ 1089 Intervals【合并n个区间/贪心】

    There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those interv ...

  2. poj 1201 Intervals 解题报告

    Intervals Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Submit Statu ...

  3. POJ 3680 Intervals(费用流)

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5762   Accepted: 2288 Descrip ...

  4. POJ 1201 Intervals (差分约束系统)

    题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...

  5. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  6. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  7. POJ 1201 Intervals(图论-差分约束)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Descri ...

  8. 图论(差分约束系统):POJ 1201 Intervals

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24099   Accepted: 9159 Descri ...

  9. 网络流(最大费用最大流) :POJ 3680 Intervals

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7218   Accepted: 3011 Descrip ...

随机推荐

  1. (转)jQuery LigerUI 插件介绍及使用之ligerTree

    一,简介  ligerTree的功能列表: 1,支持本地数据和服务器数据(配置data或者url) 2,支持原生html生成Tree 3,支持动态获取增加/修改/删除节点 4,支持大部分常见的事件 5 ...

  2. Design Pattern —— Prototype /Template Method/Iterator/Composite/Bridge

    Prototype /Template Method/Iterator/Composite/Bridge 为什么把这五种设计模式放一起呢,没什么太高大上的原因,就是因为这五种模式JAVA开发最基本的特 ...

  3. errno.h 错误码描述.

    描述:一般说的Linux源码的目录,默认是基于 /usr/include/ 的. 使用 char *strerror(int errnum); 函数打印错误代码的描述.我简单对比了一下,发现描述大体一 ...

  4. congos 日期控件的简单使用

    congos 添加html的标签,然后写上js的代码,这段代码的功能是得到前一天的日期. <input type="button" value="查询" ...

  5. Lua 基础知识-面向对象

    通过函数闭包的方式来实现面向对象 -- 通过函数闭包的方式来实现面向对象 function People(name) local self = {} local function init() sel ...

  6. UITableView实现分组, 并且点击每个分组后展开

    效果图: 简单说下实现思路: 数据传过来之后, 先创建好对应个数的分组头部View, 也就是要在 - (UIView *)tableView:(UITableView *)tableView view ...

  7. QT学习:入门篇(三)

    头文件: #ifndef CChatDisplaySet_h #define CChatDisplaySet_h #include "QtGui/QDialog" #include ...

  8. 转:初学者,手工注入测试方法小节 (出处:: 51Testing软件测试网--jie)

    1.加入单引号 ’提交,  结果:如果出现错误提示,则该网站可能就存在注入漏洞.    2.数字型判断是否有注入; 语句:and 1=1 ;and 1=2 (经典).' and '1'=1(字符型)  ...

  9. PHPCMS 插件开发教程及经验谈

    虽说 PHPCMS 开源,但其它开发文档及参考资料实在少得可怜.进行二次开发时,自己还得慢慢去研究它的代码,实在让人郁闷. PHPCMS 的“Baibu/Google地图”实在有待改进,对于数据量比较 ...

  10. CentOS 添加/绑定 IP

    美国VPS的独立IP相对于国内而言,是非常的便宜的.比如有些美国VPS,买5个独立IP才三美元左右一个月.当我们购买了多个独立IP时,如果你不想再联系客服而漫长的等待,那就自己手动配置吧. 一.进入/ ...