Cod sursa(job #467294)

Utilizator AndreyPAndrei Poenaru AndreyP Data 28 iunie 2010 13:53:10
Problema Cadrane Scor 40
Compilator cpp Status done
Runda Stelele Informaticii 2010, clasele X-XII, Ziua 2 Marime 1.71 kb
#include <cstdio>
#include <climits>
#include <bitset>
#include <algorithm>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pii pair< int,int >
#define N 100010
#define M 200010

int n;
int v[M];
pii p[N];
int yst[M],ydr[M];
bitset< M > ey;

inline int hash(int x)
{
	int pr=1,ul=v[0];
	if(x==v[pr])
		return pr;
	if(x==v[ul])
		return ul;
	int m;
	while(pr<ul)
	{
		m=(pr+ul)>>1;
		if(x<=v[m])
			ul=m;
		else
			pr=m+1;
	}
	
	if(v[pr]!=x)
	{
		if(v[pr]>x)
			--pr;
		else
			++pr;
	}
	
	return pr;
}

inline void citire()
{
	scanf("%d",&n);
	for(int i=1; i<=n; ++i)
	{
		scanf("%d%d",&p[i].fs,&p[i].sc);
		v[++v[0]]=p[i].fs;
		v[++v[0]]=p[i].sc;
	}
	
	sort(v+1,v+v[0]+1);
	int x=1;
	for(int i=2; i<=v[0]; ++i)
	{
		if(v[i]==v[x])
			continue;
		v[++x]=v[i];
	}
	v[0]=x;
	
	for(int i=1; i<=n; ++i)
	{
		p[i].fs=hash(p[i].fs);
		p[i].sc=hash(p[i].sc);
		ey[p[i].sc]=1;
		++ydr[p[i].sc];
	}
}

inline void rezolva()
{
	sort(p+1,p+n+1);
	
	int i1=1,i2=1;
	int cat;
	int r1;
	int r=0;
	int aux;
	while(i2<=n)
	{
		while(i1<i2)
		{
			--ydr[p[i1].sc];
			++i1;
		}
		
		cat=p[i1].fs;
		for(; i2<=n && p[i2].fs==cat; ++i2)
			++yst[p[i2].sc];
		
		v[1]=yst[1];
		for(int i=2; i<=v[0]; ++i)
			v[i]=v[i-1]+yst[i];
		cat=ydr[v[0]];
		if(ey[v[0]]==1)
			r1=cat+v[v[0]];
		else
			r1=INT_MAX;
		for(int i=v[0]-1; i>0; --i)
		{
			cat+=ydr[i];
			if(ey[i]==0)
				continue;
			aux=cat+v[i];
			if(aux<r1)
				r1=aux;
		}
		
		if(r1>r)
			r=r1;
	}
	
	printf("%d\n",r);
}

int main()
{
	freopen("cadrane.in","r",stdin);
	freopen("cadrane.out","w",stdout);
	
	citire();
	rezolva();
	
	return 0;
}