2020牛客暑期多校训练营(第九场)—— Groundhog Looking Dowdy

2020牛客暑期多校训练营(第九场)—— Groundhog Looking Dowdy

输入

4 3
1 3
2 8 6
1 2
3 1 7 5

输出

2

说明

Apple will pay attention to Groundhog's clothes on day 1, 3, and 4 ,Groundhog will wear clothes with dowdiness of 3, 2, and 1 on day 1, 3, and 4,and the difference is 2.

备注

1≤ai,j​≤10^9,1≤n≤10^6,1≤m≤n,the sum of clothes ≤2⋅10^6⋅ki≥1

题目大意

      有n天,每天穿一件衣服,第 i 天有 ki 件衣服可以穿,穿第 j 件衣服的的权值为 a i,j。从 n 天中选择 m 天,求这 m 天中,所穿衣服的权值最大与最小值的最小差是多少。

题解

本题请注意二维数组开不下!

由于要最小化最大值和最小值的差值,因此我们可以把所有衣服按照dowdiness从小到大排个序。

排序之后,设最终选出的m件衣服最小覆盖区间为[L,R],则答案为downdiness[R]-downdiness[L]

则一个合法的区间至少需要包含m种不同的日期

可以对于每个L求出最小的合法的R,这就转化为一个简单的尺取问题了。

若使用基数排序,可以做到在O(∑ki) 时间求解.


懂了吗?要是懂了你也不会来看题解了。

我们只能开一维的数组,此时我们可以开结构体记录是第几天穿的和的权值。

此时根据权值来排序,这样就转化为尺取问题了。

AC Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=2e6+6;
struct node{int id;int sum;}a[MAXN];
bool cmp(node x,node y){return x.sum<y.sum;}
int n,m,k,len,v[MAXN];
int ans=1e9;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&k);
		for(int j=1;j<=k;++j)
		{
			scanf("%d",&a[++len].sum);
			a[len].id=i;
		}
	}
	sort(a+1,a+len+1,cmp);
	int l=1,r=0,tmp=0;
	while(tmp<m)
	{
		r++;
		if(!v[a[r].id])
		{
			v[a[r].id]++;
			tmp++;
		}
	}
	for(l,r;r<=len;)
	{
		ans=min(ans,a[r].sum-a[l].sum);
		v[a[l].id]--;
		tmp--;
		l++;
		if(!v[a[l].id]) v[a[l].id]++,tmp++;
		while(tmp<m&&r<=len)
		{
			r++;
			if(!v[a[r].id])
			{
				v[a[r].id]++;
				tmp++;
			}
		}
	}
	printf("%d\n",ans);
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值