【链接】
bzoj1293
【解题报告】
跟NOIP2016普及组T3一样。。。
维护一段有 m 种颜色的区间,然后挑小的距离就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000005,maxm=65,INF=((1<<30)-1)*2+1;
int n,m,now,ans,num[maxm];
struct Point
{
int s,id;
bool operator < (const Point &a) const{
return s<a.s;
}
}a[maxn];
int main()
{
freopen("1293.in","r",stdin);
freopen("1293.out","w",stdout);
scanf("%d%d",&n,&m); n=0; ans=INF;
for (int i=1,len; i<=m; i++)
{
scanf("%d",&len);
for (int j=1; j<=len; j++) scanf("%d",&a[++n].s),a[n].id=i;
}
sort(a+1,a+1+n); now=0;
memset(num,0,sizeof(num));
for (int i=1,j=1; j<=n; j++)
{
if (!num[a[j].id]) now++;
num[a[j].id]++;
while (i<=j&&num[a[i].id]>1) num[a[i].id]--,i++;
if (now==m&&a[j].s-a[i].s<ans) ans=a[j].s-a[i].s;
}
printf("%d",ans);
return 0;
}

本文介绍了解决BZOJ1293问题的方法,通过维护一段包含特定种类颜色的区间,并从中挑选最小距离的方式进行解答。利用自定义的数据结构Point来存储颜色值及其标识,通过排序和滑动窗口技巧实现高效求解。
1938

被折叠的 条评论
为什么被折叠?



