[优先队列][贪心]JZOJ 6274 梦境

Description

 

Input

Output

 

Sample Input

2 2
1 3
2 4
1
3

Sample Output

2
 

Data Constraint

 

Hint

分析

把区间按照左端点排序,然后枚举转折点,把右端点最靠前的包含转折点的区间与其匹配一定最优(正确性难证)

最靠前可以用优先队列维护,时间复杂度nlogn

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int N=2e5+10;;
int n,m;
struct Intervals {
    ll l,r;
    friend bool operator < (Intervals a,Intervals b) {
        return a.r>b.r;
    }
}a[N];
ll b[N],ans;
bool vis[N];
priority_queue<Intervals> q;

bool CMP(Intervals a,Intervals b) {
    return a.l<b.l;
}

int main() {
    freopen("dream.in","r",stdin);
    freopen("dream.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].l,&a[i].r);
    for (int i=1;i<=m;i++) scanf("%lld",&b[i]);
    sort(b+1,b+m+1);sort(a+1,a+n+1,CMP);
    for (int i=1,j=1;i<=m;i++) {
        while (a[j].l<=b[i]&&j<=m) q.push(a[j]),j++;
        while (!q.empty()&&q.top().r<b[i]) q.pop();
        if (!q.empty()) ans++,q.pop();
    }
    printf("%lld",ans);
}
View Code

 

posted @ 2019-08-09 07:26  Vagari  阅读(124)  评论(0编辑  收藏  举报