Integer Intervals
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
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
//本题属于区间选两点的问题
/*AC*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct data
{
int l;
int r;
};
bool cmp(struct data a,struct data b)
{
if (a.r!=b.r)
return a.r<b.r;
return a.l>b.l;
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
vector<struct data> v(10000);
int i;
for (i=0;i<n;i++)
{
scanf("%d%d",&v[i].l,&v[i].r);
}
sort(v.begin(),v.begin()+n,cmp);
int tr=v[0].r,tl=v[0].r-1,num=2;
for (i=1;i<n;i++)
{
if (v[i].l<=tl)
continue;
if (v[i].l<=tr&&v[i].l>tl)
{
num++;
tl=tr;
tr=v[i].r;
}
if (v[i].l>tr)
{
num+=2;
int t;
tr=v[i].r;
tl=v[i].r-1;
}
}
printf("%d\n",num);
}
return 0;
}