2020牛客多校第九场 F-Groundhog Looking Dowdy

Groundhog Looking Dowdy

链接

题意

给定n,m,后接n行数据,每一行数据代表一天,第一个数是ki,代表后面有ki个数。
现在要求选择m天,其中每一天选择一个数,使得所有选择的数最大值减最小值最小。

思路

双指针算法即可。
先把所有数据按照{大小,第几天}排好序(这里可以使用O(n)的基数排序),一个指针指向左边,一个指向右边,慢慢推进即可。

代码

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define PII pair<int,int>
#define fi first
#define se second

int T;

void read(int &x){
	x=0;
	int f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
}
const int N=2e6+10;
PII p[N];
int cnt;
unordered_map<int,int>ma;
bool vis[N];
void solve(){
	int n,m;
	read(n);
	read(m);
	for(int i=0;i<n;i++){
		int k;
		read(k);
		for(int j=1;j<=k;j++){
			int tmp;
			read(tmp);
			p[cnt++]={tmp,i};
		}
	} 
	sort(p,p+cnt);
	PII l,r;
	int ans=2e9,now=0,i=0,j=0;
	
	for(i=0;i<cnt;i++){
		l=p[i];
		if(i>0){
			ma[p[i-1].se]--;
			if(!ma[p[i-1].se]){
				now--;
			}
		}
		for(;j<cnt;j++){
			r=p[j];
			if(!vis[j]){
				vis[j]=1;
				if(!ma[p[j].se]){
					now++;
				}
				ma[p[j].se]++;
			}
			if(now==m){
				ans=min(ans,r.fi-l.fi);
				break;
			}
		}
	}
	printf("%d\n",ans);
}

int main(){
//	read(T);
	T=1;
	while(T--){
		solve();
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值