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个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
随机推荐
- VS Code 用户自定义代码片段(React)
VS Code 用户自定义代码片段(React) .jsxReact组件模板:javascriptreact.json { "Import React": { "pref ...
- 深入浅出:了解JavaScript的六种继承
了解继承前我们需要了解函数的构造,方便我们理解. 常见六种继承方式: 1.原型继承call和apply: 2.原型拷贝:循环父函数protype的key值=子函数prototype的key值: 3.原 ...
- 【原创】数据处理中判断空值的方法(np.isnan、is np.nan和pd.isna)比较
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/10869725.html 1.np.isnan(只有数组数值运算时可使用) 注意:numpy模块的i ...
- java util - 时间工具包 PrettyTime
需要 prettytime-3.2.3.Final.jar 包 代码例子 package cn.java.prettytime; import java.util.Date; import java. ...
- Laravel 命令行常用命令
一.简介 1.Artisan 是 Laravel 自带的命令行接口名称,它为我们在开发过程中提供了很多有用的命令.想要查看所有可用的Artisan命令,可使用list命令: php artisan l ...
- Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码
题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...
- notification 使用的基本方法
当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...
- Sql日期时间格式转换(转 子夜.)
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- [项目1] bloger - day1
项目代码:https://github.com/venicid/Project1--Bloger 1.准备工作 1.创建project PS C:\Users\Administrator\Deskto ...
- Falsk
flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...