【集训DAY6】Dream【优先队列】【贪心】

48 篇文章 0 订阅
10 篇文章 0 订阅

在这里插入图片描述

思路:

我们发现可以直接贪心,即选包含当前点的区间中右端点最小的,可以直接用堆来维护

c o d e code code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>

#define ll long long

using namespace std;

const ll MAXN = 2e5 + 10;

ll n, m;
ll s[MAXN];

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

priority_queue<int> q;

bool cmp(node x, node y) {
	return x.x < y.x;
}

int main() {
	freopen("dream.in", "r", stdin);
	freopen("dream.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for(ll i = 1; i <= n; i ++) scanf("%d%d", &a[i].x, &a[i].y);
	for(ll i = 1; i <= m; i ++) scanf("%d", &s[i]);
	sort(s + 1, s + 1 + m);
	sort(a + 1, a + 1 + n, cmp);
	ll sum = 0;
	for(ll i = 1, j = 1; i <= m; i ++) {
		while(a[j].x <= s[i] && j <= n) q.push(-a[j].y), j++;
		while(!q.empty() && -q.top() < s[i]) q.pop();
		if(!q.empty()) {
			q.pop();
			sum ++;
		}
	}
	printf("%d", sum);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值