Practice Day4 T1

· · 个人记录

Practice Day4 T1

---------(luogu U326287)

引入

最先想到的就是人选出口。

将出口从大到小排序,人刚好相反。人每次选择离其最近且满足条件的出口。

这样很难实现。

正解

转化思路,出口选人。

将出口和人放在一起,按 x 从大到小排序,这样将 x 这维去掉,只需考虑 y 维。

随后从 1 开始扫描,如果是出口,则将一个权值为 y 的数加入数据结构。

如果是人,则取出数据结构中恰好比人大于或等于的数取出,成功则 ++ans。

这里的数据结构结构可以用 set,vector 等结构。

代码如下。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n,m,num,ans;
set<ll> st;
struct node
{
    ll x,y,z;
} a[200009];

bool cmp(const node&p,const node&q)
{
    return p.x>q.x;
}

signed main()
{
    scanf("%lld %lld",&num,&n);
    for(int i=1; i<=n; ++i)
    {
        scanf("%lld %lld",&a[i].x,&a[i].y);
        a[i].z=1;
    }
    for(int i=1; i<=n; ++i)
    {
        scanf("%d%d",&a[n+i].x,&a[n+i].y);
        a[n+i].z=2;
    }
    sort(a+1,a+2*n+1,cmp);
    for(int i=1; i<=2*n; ++i)
    {
        if(a[i].z==2)
            st.insert(a[i].y);
        else
        {
            auto t=(st.lower_bound(a[i].y));
            if(t==st.end())
            {
                continue;
            }
            ++ans;
            st.erase(*t);
        }
    }
    printf("%lld\n",ans);
    return 0;
}