【JZOJ6274】梦境

description


analysis

  • 其实可以贪心

  • 先把区间按左端点排序,转折点也排序

  • 扫一次转折点,把所有左端点在当前点左边的区间丢进优先队列里

  • 按照贪心策略,对于某个转折点,一定选择右端点离它最近的区间

  • 于是把不合法(右端点在转折点左边)的区间弹出,匹配下去就好了


code

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define MAXN 200005
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)

using namespace std;

priority_queue <ll,vector<ll>,greater<ll> > q;
ll n,m,now=1,ans;
ll b[MAXN];

struct node
{
	ll x,y;
}a[MAXN];

inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
	while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
inline bool cmp(node a,node b){return a.x<b.x;}
int main()
{
	freopen("T2.in","r",stdin);
	//freopen("dream.in","r",stdin);
	//freopen("dream.out","w",stdout);
	n=read(),m=read();
	fo(i,1,n)a[i].x=read(),a[i].y=read();
	fo(i,1,m)b[i]=read();
	sort(a+1,a+n+1,cmp),sort(b+1,b+m+1);
	fo(i,1,m)
	{
		while (a[now].x<=b[i] && now<=n)q.push(a[now++].y);
		while (!q.empty() && b[i]>q.top())q.pop();
		if (!q.empty() && b[i]<=q.top())++ans,q.pop();
	}
	printf("%lld
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/horizonwd/p/11316253.html