[BZOJ1293][SCOI2009]生日礼物(单调队列)

题目描述

传送门

题解

两个指针扫一遍就行了。

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 1000005
#define inf 2100000000

int n,k,m,x,cnt,l,r,now,ans;
struct hp{int col,loc;}a[N];
int p[105];

int cmp(hp a,hp b)
{
    return a.loc<b.loc;
}
void inc(int i)
{
    if (!p[a[i].col]) ++now;
    ++p[a[i].col];
}
void dec(int i)
{
    --p[a[i].col];
    if (!p[a[i].col]) --now;
}
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=k;++i)
    {
        scanf("%d",&m);
        for (int j=1;j<=m;++j)
        {
            scanf("%d",&x);
            a[++cnt].col=i,a[cnt].loc=x;
        }
    }
    sort(a+1,a+cnt+1,cmp);
    l=r=1;inc(1);ans=inf;
    while (l<=n)
    {
        while (now<k&&r<n)
        {
            ++r;
            inc(r);
        }
        if (now>=k) ans=min(ans,a[r].loc-a[l].loc);
        dec(l);++l;
    }
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值