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