BZOJ 1293 SCOI2009 生日礼物 堆

22 篇文章 0 订阅

题目大意:给定一个数轴上n个点,每个点有一种颜色,一共k种颜色,求一个最短的区间,包含所有k种颜色

卡了一段时间0.0 一开始想二分答案啥的 后来发现数据范围太大写不了0.0 后来去找题解才发现尼玛真巧妙

维护一个堆 将每种颜色的第一个珠子加入堆 然后不断把最左侧的珠子取出,加入该种颜色的下一个 同时更新ans

果然这么大数据范围还是要用堆这种常数小的数据结构啊0.0

我手写了堆却开了STL的queue 0.0 不要说我有病我只是不习惯STL的堆罢了

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> abcd;
int n,m,k,top,ans=0x7fffffff,maxnum=0;
abcd heap[70];
queue<int>q[70];
void Insert(abcd x)
{
	heap[++top]=x;
	int t=top;
	while( t>1 && heap[t]<heap[t>>1] )
		swap(heap[t],heap[t>>1]),t>>=1;
}
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,j,x;
	cin>>n>>k;
	for(i=1;i<=k;i++)
	{
		scanf("%d",&m);
		for(j=1;j<=m;j++)
			scanf("%d",&x),q[i].push(x);
		Insert(abcd(q[i].front(),i));
		maxnum=max(maxnum,q[i].front());
		q[i].pop();
	}
	ans=min(ans,maxnum-heap[1].first);
	while(1)
	{
		abcd temp=heap[1];Pop();
		if(q[temp.second].empty())
			break;
		Insert(abcd(q[temp.second].front(),temp.second));
		maxnum=max(maxnum,q[temp.second].front());q[temp.second].pop();
		ans=min(ans,maxnum-heap[1].first);
	}
	cout<<ans<<endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值