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. ssanf()的用法

    转载自http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html 看着写着不错,就转了,保存一下 C语言函数sscanf()的用法 ss ...

  2. TCP Linger的坑

    昨天和同事奋战几个小时,解决了一个linger造成的bug. 现象是这样的,这是一个我从原型接手,扩充了各种功能成为可用代码的epoll实现的非阻塞socket server程序,接收大量的短连接,测 ...

  3. JavaScript中几个可以转化为false的值

    1.[0,NaN,“”,null,undefined]都可以直接转化为false,但这几个值不是完全相等的 var arr = [0,"",false,null,undefined ...

  4. angularjs使用directive实现分页组件

    闲来没事,分享下项目中自己写的分页组件.来不及了,直接上车. 效果: 输入框可任意输入,并会自动提交到该页 html: <ul class="page clearfix"&g ...

  5. CSS的力量

    CSS(Cascading Style Sheet)级联样式表,是一种美观网页设计的解决方案,也是W3C推荐的标准,他可以是我们的网页设计更灵活,更美观,使设计人员对内容的设计和样式的设计分离,使设计 ...

  6. [每日一题] OCP1z0-047 :2013-07-24 子查询――外查询与内查询的执行顺序

    一.Oracle的子查询分为两类分别是嵌套子查询和非嵌套子查询.所谓嵌套子查询是指,子查询是一个独立的查询不与外部查询相关,子查询将被先执行,而且只被执行一次,子查询执行完成后,再执行外部的查询,外部 ...

  7. 多线程08-Callable和Future

    1.简介 Callable是一个接口,与Runnable类似,包含一个必须实现的call方法,可以启动为让另一个线程来执行,执行Callable可以得到一个Future对象 该对象可以监听Callab ...

  8. zabbix log(logrt) key的使用

    今天看了篇帖子是关于如何利用zabbix 自带的key去读log,监控linux异常登陆,一直以来都是自己写脚本去读log的.就想看看这个zabbix log 这个key怎么样..好吧开始: 官方文档 ...

  9. 1.3.1 switch 语句中的 String

    switch语句是一种高效的多路语句,可以省掉很多繁杂的嵌套if判断: 在Java 6及之前,case语句中的常量只能是byte.char.short和int(也可以是对应的封装类)或枚举常量,在Ja ...

  10. Remoting通讯实例

    Remoting通讯可以实现不同应用程序域之间的通信(可以为一台机器不同程序,也可以不同机器上的不同程序) 实例步骤: (1)编写透明代理类 新建一个类库,命名空间:MyRemoting 在该类库中建 ...