POJ1716 Integer Intervals
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13984 | Accepted: 5943 |
Description
Write a program that: finds the minimal number of elements in a set
containing at least two different integers from each interval.
Input
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
Sample Input
4
3 6
2 4
0 2
4 7
Sample Output
4
Source
题目和POJ1201相同。
由于这次区间内的数的个数固定为2,所以可以使用贪心解法。将区间按右端点从小到大排序,每次能不加点就不加,必须加的话就加在区间最右面,并累计答案数。
/**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
struct area{
int l,r;
}a[mxn];
int n;
int ans,f,s;
int cmp(area a,area b){
if(a.r!=b.r)return a.r<b.r;
return a.l<b.l;
}
int main(){
scanf("%d",&n);
int i,j;
for(i=;i<=n;i++)scanf("%d%d",&a[i].l,&a[i].r);
sort(a+,a+n+,cmp);
ans=;f=a[].r;s=a[].r-;
for(i=;i<=n;i++){
if(f<a[i].l){
ans+=;
f=a[i].r;
s=a[i].r-;
continue;
}
if(s<a[i].l){
ans++;
if(f>a[i].r-) s=a[i].r-;
else s=f;
f=a[i].r;
continue;
}
if(f>a[i].r)f=a[i].r;
}
printf("%d\n",ans);
return ;
}
POJ1716 Integer Intervals的更多相关文章
- poj1716 Integer Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Integer Intervals Time Limit: 1000MS Me ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- poj 1716 Integer Intervals(差分约束)
1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...
- 【poj1716】 Integer Intervals
http://poj.org/problem?id=1716 (题目链接) 题意 给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数. Solution 差分约束. 运用前 ...
- Integer Intervals(贪心)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12123 Accepted: 5129 Description An i ...
- POJ 1716 Integer Intervals
题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...
- POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
随机推荐
- 使用Maven开发一个简单的SpringData
1:创建Maven项目 2:添加依赖(修改pom.xml为以下代码) <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- 安装并配置多实例Mysql数据库
1.安装Mysql需要的依赖包 yum -y install ncurses-devel libaio-devel cmake 2.创建Mysql用户账号 useradd -s /sbin/nolog ...
- 设置虚拟机里的Centos7的IP
输入ip查询命名 ip addr 也可以输入 ifconfig查看ip,但此命令会出现3个条目,centos的ip地址是ens33条目中的inet值. 发现 ens33 没有 inet 这个属性,那 ...
- 【jenkins】【java】【tomcat】windows host key verification failed
适用于windows系统 出现这个问题的原因tomcat启动的用户找不到本地公私钥路径 如果tomcat 启动时候选择 local system account (默认此选项),请把你的公私钥文件(i ...
- python中生成器对象和return 还有循环的区别
python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...
- 关于json数据中的多反斜杆转译--StringEscapeUtils.unescapeJava(踩过的坑)
一.需求 现有一个字符串str String str = "{\\\"name\\\":\\\"spy\\\",\\\"id\\\\&quo ...
- Java语言基础---逻辑运算(长路短路运算)
长路短路运算的区别 长路与运算&:是指在两边都是整数时,是逐位与运算,在两边是关系运算时,是逻辑运算. 短路与运算&&:是指从左至右,遇到false,则停止后面的运算. 长路或 ...
- 4444: [Scoi2015]国旗计划
4444: [Scoi2015]国旗计划 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 485 Solved: 232 Description A国 ...
- 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist
不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句: ./spark-shell --master spark://ma ...
- leetcode 【 Reorder List 】python 实现
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do ...