#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
*/
POJ 3190 Stall Reservations
最新推荐文章于 2024-07-22 09:38:37 发布