Stall Reservations(POJ 3190, 贪心)

题目链接

       我感觉我该去看紫书研究研究STL了,不知道的东西太多了,不会优先队列的话应该是做不了这个题吧,因为既在按开始时间排序之后判断每个cow是否需要新的stall或者说需要用哪个stall的时候需要找之前的cow中B最早的,自己找的话貌似只能用复杂度很高的方法吧,而优先队列插入数据的复杂度为O(lgn),删除数据的复杂度为O(1),这里n的最大值即stall的最大数,而stall的最大值也不会超过N,所以这个算法的复杂度应为O(N*lgN)。

       此题是贪心加优先队列,具体看代码吧。

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

using namespace std;

const int MAX_N = 50005;

struct cow {
	int num, A, B, stall;
	friend bool operator <(cow b, cow c)
	{
		return b.B>c.B;	
	} 
} a[MAX_N];

int N, stall;

bool cmp(cow b, cow c){
	return b.A < c.A;
}
void ini()
{
	for(int i=0; i<N; i++){
		a[i].num = i;
		scanf("%d%d", &a[i].A, &a[i].B);
	}
	stall = 1;
}
void assign()
{
	priority_queue<cow> que;
	a[0].stall = 1;
	que.push(a[0]);
	for(int i=1; i<N; i++){
		cow temp = que.top();
		if(a[i].A > temp.B){
			que.pop();
			a[i].stall = temp.stall;
		}
		else a[i].stall = ++stall;
		que.push(a[i]);
	}
}
void print()
{
	printf("%d\n", stall);
	int b[MAX_N];
	for(int i=0; i<N; i++){
		b[a[i].num] = a[i].stall;
	}
	for(int i =0; i<N; i++){
		printf("%d\n", b[i]);
	}
}
int main()
{
	while(~scanf("%d", &N)){
		ini();
		sort(a, a+N, cmp);
		assign();
		print();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值