2018.10.10 bzoj1293: [SCOI2009]生日礼物(双指针)

传送门
把所有点按坐标排序,用尺取法(双指针)跑一遍就行了。
代码:

#include<bits/stdc++.h>
#define N 1000005
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
int n,k,ans=2e9,tot=0,cnt[70],colors=0;
struct Node{int col,pos;}p[N];
inline bool cmp(Node a,Node b){return a.pos<b.pos;}
int main(){
	n=read(),k=read();
	for(int i=1;i<=k;++i){
		int t=read();
		while(t--)p[++tot]=(Node){i,read()};
	}
	sort(p+1,p+n+1,cmp);
	for(int l=1,r=0;l<=n;){
		while(colors<k&&r<n){
			++r,++cnt[p[r].col];
			if(cnt[p[r].col]==1)++colors;
		}
		while(colors>=k){
			ans=min(ans,p[r].pos-p[l].pos);
			--cnt[p[l].col];
			if(!cnt[p[l].col])--colors;
			++l;
		}
		if(r==n)break;
	}
	cout<<ans;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值