• BZOJ 1293 SCOI2009 生日礼物 堆


    题目大意:给定一个数轴上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;
    }
    


  • 相关阅读:
    设计模式(18)>职责链模式 小强斋
    设计模式(17)>中介者模式 小强斋
    设计模式(16)>原型模式 小强斋
    设计模式(20)>状态模式 小强斋
    设计模式(15)>桥接模式 小强斋
    设计模式(18)>职责链模式 小强斋
    堆栈的工作原理
    掌握udev
    Qt Model/View 学习笔记 (七)
    Qt Model/View 学习笔记 (六)
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7239830.html
Copyright © 2020-2023  润新知