poj 1716
来源:互联网 发布:天猫化妆品销售数据 编辑:程序博客网 时间:2024/06/08 10:51
Integer Intervals
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11866 Accepted: 5007
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.
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
43 62 40 24 7
Sample Output
4
Source
CEOI 1997
0 1 2 3 4 5 6 7----- (0 to 2) ----- (2 to 4) ------- (3 to 6) ------- (4 to 7)
数据如上,要找的就是一个集合,集合与数据的每个集合的交集至少有2个元素,求集合最少的数的个数。
贪心:先按照结尾数排序,第一个集合找末尾2个数,第二个集合,现根据前面判断已经有几个数在交集中了,如果够2个就跳到第三个集合,如果不够就从末尾找,往前补够2个,第三及以后的类推。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ int a,b;}p[10005];int n,ans;bool cmp(node x,node y){ return x.b<y.b;}bool v[10005];int main(){ while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++)scanf("%d%d",&p[i].a,&p[i].b); sort(p,p+n,cmp); memset(v,0,sizeof(v)); v[p[0].b-1]=1; v[p[0].b]=1; ans=2; for(int i=1;i<n;i++) { int num=2; for(int j=p[i].a;j<=p[i].b;j++) { if(v[j]==1)num--; } if(num>0) while(num) { v[p[i].b-num+1]=1; ans++; num--; } } cout<<ans<<endl;} return 0;}
- poj 1716
- POJ 1716 Integer Intervals
- poj 1716 Integer Intervals
- POJ 1716 Integer Intervals
- poj 1716-Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- poj 1716(贪心)
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ 1716 Integer Intervals
- POJ-1716 Integer Intervals
- poj-1716【贪心】
- poj 1716 Integer Intervals
- POJ
- poj
- POJ
- POJ
- 三个简单排序算法的java实现
- Android(Java):ndk开发
- 原型模式
- coding - 递减数组左移后寻找某一个数
- alter table
- poj 1716
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- Ubuntu linux-gcc安装与编译
- AMR-NB 与 AMR-WB 语音编码标准技术的对比研究
- 【转载】寻找真正的入口(OEP)--广义ESP定律
- drop 用法
- HDU-1396 神奇的三角形。坑!
- Android 动态logo bootanimation.zip 制作
- 如何在C#中使用get及set方法定义属性