poj1716 Integer Intervals(贪心)

来源:互联网 发布:excel筛选重复数据个数 编辑:程序博客网 时间:2024/05/16 14:42
Integer Intervals
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12260 Accepted: 5178

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

43 62 40 24 7

Sample Output

4

 

题意:

给出n个int连续闭区间,求一点集元素的最小数目,该集合与各给出区间的交集至少含两元素

思路:

见代码

#include<stdio.h>#include<algorithm>using namespace std;struct ss{int a,b;} s[10004];int cmp(ss x,ss y){return x.b<y.b;//按右端点升序排列}int main(){int n,i,ans,x1,x2;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)scanf("%d%d",&s[i].a,&s[i].b);sort(s,s+n,cmp);ans=2;//点集初始数目x1=s[0].b-1; x2=s[0].b;//集合内初始元素for(i=1;i<n;i++){if(x2>=s[i].a&&x1<s[i].a){//集合内仅有最新一个元素属于该区间x1=x2;x2=s[i].b;ans++;}else if(x2<s[i].a){//集合与该区间无交集x1=s[i].b-1;x2=s[i].b;ans+=2;}}printf("%d\n",ans);}return 0;}


 

0 0
原创粉丝点击