BZOJ 1528 POI2005 sam-Toy Cars 堆+贪心

24 篇文章 0 订阅
22 篇文章 0 订阅

题目大意:有n个玩具,都放在架子上,地板上能放k个,要玩p次玩具,如果不在地板上就要去架子上拿,地板满了要放回去,求最少操作次数

贪心思想:每次放回玩具时选择下次玩的时间最靠后的玩具放回去

可以用堆来模拟这一贪心过程

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 500500
using namespace std;
typedef pair<int,int> abcd;
int n,k,p,cnt,ans;
int a[M],last[100100],next[M];
bool on_floor[100100];
namespace Priority_Queue{
	abcd heap[M];int top;
	void Insert(abcd x)
	{
		heap[++top]=x;
		int t=top;
		while(t>1)
		{
			if(heap[t]>heap[t>>1])
				swap(heap[t],heap[t>>1]),t>>=1;
			else
				break;
		}
	}
	void Pop()
	{
		heap[1]=heap[top--];
		int t=2;
		while(t<=top)
		{
			if(t<top&&heap[t+1]>heap[t])
				++t;
			if(heap[t]>heap[t>>1])
				swap(heap[t],heap[t>>1]),t<<=1;
			else
				break;
		}
	}
}
int main()
{
	int i;
	cin>>n>>k>>p;
	for(i=1;i<=p;i++)
	{
		scanf("%d",&a[i]);
		if(last[a[i]]) next[last[a[i]]]=i;
		last[a[i]]=i;
	}
	for(i=1;i<=n;i++)
		if(last[i])
			next[last[i]]=0x3f3f3f3f;
	for(i=1;i<=p;i++)
	{
		if(on_floor[a[i]])
		{
			Priority_Queue::Insert(abcd(next[i],a[i]));
			continue;
		}
		if(cnt==k)
		{
			on_floor[Priority_Queue::heap[1].second]=0;
			Priority_Queue::Pop();--cnt;
		}
		Priority_Queue::Insert(abcd(next[i],a[i]));
		on_floor[a[i]]=1;++ans;++cnt;
	}
	cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值