P2859 [USACO06FEB]Stall Reservations S

知识点:贪心

贪心的策略,用优先队列维护栅栏,每次都让结束时间最小的栅栏在堆顶,如果堆顶的不满足要求那么新建立一个栅栏入堆,

#include <bits/stdc++.h>

using namespace std;

const int N = 5e4 + 5;

struct node {
	int x, y, r;
} a[N];

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

struct cmp2 {
	bool operator () (pair<int, int> a, pair<int, int> b) {
		if (a.second != b.second) return a.second > b.second;
		else return a.first > b.first;
	}
};

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i].x >> a[i].y;
		a[i].r = i;
	}
	sort(a, a + n, cmp);
	int ans[N];
	int tot = 1;
	priority_queue<pair<int, int>, vector<pair<int, int>>, cmp2> q;
	q.push(make_pair(tot, a[0].y));
	ans[a[0].r] = tot++;
	for (int i = 1; i < n; i++) {
		pair<int, int> t = q.top();
		if (t.second < a[i].x) {
			q.pop();
			t.second = a[i].y;
			q.push(t);
			ans[a[i].r] = t.first;
		} else {
			q.push(make_pair(tot, a[i].y));
			ans[a[i].r] = tot++;
		}
	}
	cout << tot - 1 << endl;
	for (int i = 0; i < n; i++) cout << ans[i] << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值