Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12123   Accepted: 5129

Description

An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

Input

The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4

题意:在数轴上给n个区间,区间上的点均是整数,再在数轴上取x个点构成的点集V使得V与上述每个区间的交集至少包含两个交点,输出满足题意的x的最小值。
思路:可以贪心,先按每个区间的有端点升序排序,
初始化:计数器 sum = 2,集合V的前两个元素selem,telem为第一个区间的最后两个整数;(selem和telem在整个循环中作为集合V的最后两个元素,所以必要时更新其值)
for:
  若第i+1个区间包含selem和telem,不需要做任何改变;
  若第i+1个区间只包含telem,更新selem和telem,sum加1;
  若第i+1个区间不包含selem和telem,更新selem和telem,sum加2;
 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; struct node
{
int s,t;
}L[];
int cmp(const struct node &a, const struct node &b)
{
return a.t < b.t;
} int main()
{
int n;
scanf("%d",&n);
for(int i = ; i < n; i++)
scanf("%d %d",&L[i].s,&L[i].t);
sort(L,L+n,cmp);
int sum,selem,telem;
sum = ;
selem = L[].t-;
telem = L[].t;
for(int i = ; i < n;)
{
if(selem >= L[i].s && selem <= L[i].t && telem >= L[i].s && telem <= L[i].t)
i++;
else if(selem < L[i].s && telem >= L[i].s && telem <= L[i].t)
{
selem = telem;
telem = L[i].t;
sum++;
i++;
}
else if(selem < L[i].s && telem < L[i].s)
{
selem = L[i].t-;
telem = L[i].t;
sum += ;
i++;
}
}
printf("%d\n",sum);
return ;
}

												

Integer Intervals(贪心)的更多相关文章

  1. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  2. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  3. poj1716 Integer Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Integer Intervals Time Limit: 1000MS   Me ...

  4. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  5. poj 1716 Integer Intervals(差分约束)

    1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...

  6. POJ1716 Integer Intervals

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13984   Accepted: 5943 Description An i ...

  7. E - Intervals 贪心

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  8. POJ 1716 Integer Intervals

    题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...

  9. 【poj1716】 Integer Intervals

    http://poj.org/problem?id=1716 (题目链接) 题意 给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数. Solution 差分约束. 运用前 ...

随机推荐

  1. Java基础知识强化之集合框架笔记24:ArrayList存储自定义对象并遍历

    1. ArrayList存储自定义对象并遍历 2. 代码示例: Student.java,如下: package cn.itcast_01; public class Student { privat ...

  2. 第七章----pwm蜂鸣器

    Linux代码的重用: 在头文件中加载即可使用到原来所有的功能,还有动态方式就是一个驱动使用另一个驱动的资源. 对于像蜂鸣器这样的实验,它的内部文件有很多,所有要有很多的源代码以用于妥善管理. 有多个 ...

  3. 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片

    项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...

  4. IOS DLNA PlatinumKit库的使用

    前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...

  5. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  6. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  7. Sublime text3 JS语法检测工具安装及使用

    Sublime text3 JS语法检测工具安装及使用 工具/原料 sublime text3 nodejs sublimeLinter sublimeLinter-jshint 方法/步骤 首先ct ...

  8. int*-------int

    a=(int)((int*)0 + 4)求a是多少 大家看图应该明白了  十六进制0x00000010转换为十进制就是16

  9. Discuz!源代码阅读笔记之common.inc.php文件【1】

    <?php /* [Discuz!] (C)2001-2007 Comsenz Inc. This is NOT a freeware, use is subject to license te ...

  10. python自动开发之第十二天

    一.数据库的介绍 (1)由多张表组成(2)存取有规则,数据有关联(3)数据量大,被优化 好处:更有效的存取数据 二.关系型数据库管理系统(RDBMS) Oracle,Mysql,Sqlserver,D ...