JZOJ5866. 【NOIP2018模拟9.13】指引

在这里插入图片描述
在这里插入图片描述

题解

就是要求,一个点,在他的右上角,有没有一个可以跟他匹配的点,
求最大的匹配数。
像这种二维偏序问题,
就先对一维进行排序,然后用数据结构维护另一维。

code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define N 100003
#define P putchar
#define G getchar
#define ls (x<<1)
#define rs (x<<1|1)
#define M ((l+r)>>1)
using namespace std;
char ch;
void read(int &n)
{
	n=0;
	ch=G();
	while((ch<'0' || ch>'9') && ch!='-')ch=G();
	int w=1;
	if(ch=='-')w=-1,ch=G();
	while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
	n*=w;
}

struct node
{
	int x,y,t;
}a[N*2];

bool cmp(node a,node b)
{
	return a.x>b.x || (a.x==b.x && a.y<b.y) || (a.x==b.x && a.y==b.y && a.t<b.t);
}

int s[N*8],n,m,ans,y;

bool find(int x,int l,int r)
{
	if(!s[x] || l>y)return 0;
	if(l==r)
	{
		s[x]--;
		return 1;
	}
	int m=M;
	if(find(rs,m+1,r))
	{
		s[x]--;
		return 1;
	}
	if(find(ls,l,m))
	{
		s[x]--;
		return 1;
	}
	return 0;
}

void ins(int x,int l,int r)
{
	s[x]++;
	if(l==r)return;
	int m=M;
	if(y<=m)ins(ls,l,m);
		else ins(rs,m+1,r);
}

int main()
{
	freopen("guide.in","r",stdin);
	freopen("guide.out","w",stdout);
	
	read(n);read(n);m=n<<1;
	for(int i=1;i<=n;i++)
		read(a[i].x),read(a[i].y),a[i].t=123,a[i].y=m-a[i].y;
	for(int i=1;i<=n;i++)
		read(a[n+i].x),read(a[n+i].y),a[n+i].t=12,a[i+n].y=m-a[i+n].y;
	
	sort(a+1,a+1+m,cmp);
	
	for(int i=1;i<=m;i++)
		if(a[i].t==123)
		{
			y=a[i].y;
			if(find(1,1,m))ans++;
		}else y=a[i].y,ins(1,1,m);
	printf("%d",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/82764935