这个题的意思是:找出一个数的集合(是离散开的)其中的第在给出的区间中至少存在两点。


像这样的题,一般都是要先进行区间的排序后做的。



其中我在网上看到了比较高明的做法,再看吧,,先贴个网址:




#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<string>

using namespace std;


int n;
struct my
{
	int x;
	int y;
	bool operator<(my b)
	{
		if (y!=b.y)
			return y<b.y;
		else
			return x<b.x;
	}
}go[11111];

int t[11111];


void update(int k)
{
	k+=2;
	while (k<11111)
	{
		t[k]++;
		k+=k&-k;
	}
}

int find(int k)
{
	k+=2;
	int sum=0;
	while (k)
	{
		sum+=t[k];
		k-=k&-k;
	}
	return sum;
}

int main()
{
	freopen("in.txt","r",stdin);
	int i,j,k;
	cin>>n;
	for (i=0;i<n;i++)
		cin>>go[i].x>>go[i].y;

	sort(go,go+n);

	memset(t,0,sizeof(t));

	int ans=0;
	for (i=0;i<n;i++)
	{
		k=find(go[i].y)-find(go[i].x-1);
		if (k==0)
		{
			ans+=2;
			update(go[i].y);
			update(go[i].y-1);
		}
		else if (k==1)
		{
			ans++;
			if (find(go[i].y)==find(go[i].y-1))
				update(go[i].y);
			else
				update(go[i].y-1);
		}
	}
	cout<<ans<<endl;
}




Integer Intervals


Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 10987

 

Accepted: 4611


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