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. Tomcat7.0.40 基于DataSourceRealm的和JDBCRealm的资源用户访问控制

    软件版本: Tomcat 7.0.40 Mysql: 5.1 Host: CentOS 6.3 64bit 使用JDBCRealm (Tomcat 会使用所给的JDBC Connecter 自己去连M ...

  2. iOS流布局UICollectionView使用FlowLayout进行更灵活布局

    一.引言 前面的博客介绍了UICollectionView的相关方法和其协议中的方法,但对布局的管理类UICollectionViewFlowLayout没有着重探讨,这篇博客介绍关于布局的相关设置和 ...

  3. SpringSecurity 在MVC 中的简单使用(翻译的,稍加改动)

    Spring Security允许开发人员轻松地将安全功能集成到J2EE Web应用程序中,它通过Servlet过滤器实现“用户自定义”安全检查. 在本教程中,我们将向您展示如何在Spring MVC ...

  4. 绝对炫的幻灯片插件-SKITTER

    绝对炫的幻灯片插件-SKITTER 所属分类:媒体-幻灯片和轮播图,图片展示,滑块和旋转 Includes code source // Styles <link href="css/ ...

  5. SQL替换空格,制表符,换行符,回车符.

    首先是空格的替换,很重要的有点是,要确保字段的类型,不是char或nchar等固定的类型,否则无法去掉空格. 去掉空格很简单,如下为SQL实例: --去掉 T_StuffBasic 表中FBranch ...

  6. 删除我的电脑中360随身WiFi云U盘的图标

    可通过删除注册表的方法 运行-regedit 找到这个项 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\My ...

  7. iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏

    一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...

  8. java 保留小数点后N位数(若干位),几种实现的方式总结

    import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;/** * java ...

  9. 微软SQLHelper.cs类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  10. [转]iframe自适应宽度高度

    <iframe id="iframe" onLoad="AutoFit();" frameborder="0" scrolling=& ...