POJ 3190 Stall Reservations G++

本文介绍了一种使用优先队列实现的任务调度算法,通过不断比较任务的开始和结束时间来分配资源,确保了资源的有效利用。该算法适用于需要对多个任务进行高效管理和调度的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
//抄博友好程序 优先队列没掌握 
int jg[50008];
struct nod{
	int l,r,id;
};
bool cmp(nod x,nod y)
{
	return x.l<y.l;
}
struct qme{
	int r,stall;
	bool operator<(const qme& a)const {//抄博友 
		return r > a.r;
	}	
};
/*
bool bj(qme x, qme y)
{
	return x.r<y.r; 
}*/
int main()
{
	int n;
	cin>>n;
	vector<nod> ve;
	for(int i=1;i<=n;i++)
	{
		nod t;
		scanf("%d %d",&t.l,&t.r);
		//cin>>t.l>>t.r;
		t.id=i;
		ve.push_back(t);
	}
	sort(ve.begin(),ve.end(),cmp);
	int js=1;//stall数量 
	priority_queue<qme> pq;
	jg[ve[0].id]=js;
	qme t;
	t.r=ve[0].r;
	t.stall=js;
	pq.push(t);
	for(int i=1;i<n;i++)
	{
		if(ve[i].l>pq.top().r)
		{
			//cout<<"hello"<<" "<<i<<endl;
			//cout<<pq.top().r<<endl;
			qme t;
			t.r=ve[i].r;
			t.stall=pq.top().stall;
			jg[ve[i].id]=pq.top().stall;
			pq.pop();
			pq.push(t);
		}else
		{
			//cout<<"hi"<<" "<<i<<endl;
			qme t;
			t.r=ve[i].r;
			js++;
			t.stall=js;	
			jg[ve[i].id]=js;		
			pq.push(t);			
		}
	} 
	printf("%d\n",js);
	//cout<<js<<endl;
	for(int i=1;i<=n;i++)
	{
		printf("%d\n",jg[i]);
		//cout<<jg[i]<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值