Integer Intervals
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12886 | Accepted: 5449 |
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
4 3 6 2 4 0 2 4 7
Sample Output
4
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int n;
struct node
{
int x,y;
}e[10010];
bool cmp(node a,node b)
{
return a.y<b.y;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int sum=0;
for(int i=0;i<n;i++)
scanf("%d%d",&e[i].x,&e[i].y);
sort(e,e+n,cmp);
int st=e[0].y-1,se=e[0].y;
for(int i=1;i<n;i++)
{
if(e[i].x<=st)
continue;
else if(e[i].x<=se)
{
sum++;
st=se;
se=e[i].y;
}
else
{
sum+=2;
st=e[i].y-1;
se=e[i].y;
}
}
printf("%d
",sum+2);
}
return 0;
}