POJ 3190 Stall Reservations

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
typedef long long LL;
const int maxn = 50005;
const LL INF = 10000000000005;
const int mod = 1e6 + 7;
using namespace std;
//------------------------------------------------------------------------------------------//
//POJ 3190 Stall Reservations
//思路:按l排序,维护一个r的最小堆,按输入顺序输出结果
//不太理解
struct A {
	int l, r;
	int idx, pos;
	bool operator<(const A &rhs) const {
		return r > rhs.r;
	}
}a[maxn];

bool cmp1(const A &a, const A &b) {
	if(a.l != b.l) return a.l < b.l;
	else return a.r < b.r;
}

bool cmp2(const A &a, const A &b) {
	return a.idx < b.idx;
}

int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		CLEAR(a, 0);
		for (int i = 0; i < n; ++i) {
			scanf("%d%d", &a[i].l, &a[i].r);
			a[i].idx = i;
		}
		sort(a, a + n, cmp1);
		priority_queue<A> stall;//放入队列中的是拷贝,不是引用
		int ans = 0;
		a[0].pos = ++ans;
		stall.push(a[0]);
		for (int i = 1; i < n; ++i) {
			if (stall.top().r < a[i].l) {
				a[i].pos = stall.top().pos;
				stall.pop();
			}
			else a[i].pos = ++ans;
			stall.push(a[i]);
		}
		sort(a, a + n, cmp2);
		printf("%d\n", ans);
		for (int i = 0; i < n; ++i) printf("%d\n", a[i].pos);
	}
	return 0;
}

/*
优先队列
因为优先出列判定为!cmp
struct cmp
{
	bool operator()(int &a, int &b) const
	{
		return d[a] > d[b];
	}
};
对于内置类型,可以采用自定义比较结构来改变默认值,注意要const
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值