hdu4-Simple Set Problem(尺取法)

Problem - 7314 (hdu.edu.cn)

Simple Set Problem

从所给的每个数组中选一个数组成新数组,再用新数组的最大值减去最小值,使得差最小

 尺取法:

​​​​​​尺取法介绍_nefu_0iq的博客-CSDN博客

线性结构——尺取法_尺取法时间复杂度_Alex_McAvoy的博客-CSDN博客

一维数组或序列中寻找满足特定条件的子区间的方法。通过设置两个指针,即"尺"和"取",来动态调整区间范围,从而快速地找到满足条件的子区间。 

通常适用于需要在连续子区间中寻找最优解或满足一定条件的问题,例如求最长连续递增子数组

*单调序列

3061 -- Subsequence (poj.org)

Problem - 5178 (hdu.edu.cn)

Problem - 1937 (hdu.edu.cn) //二维数组

2566 -- Bound Found (poj.org) //前缀和

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;

int n,k,t,x,y,l,r;

int vis[N],cnt,mind;
struct node{
	int v,id;
	bool operator<(const node &y)const{
		if(v!=y.v)return v<y.v;
		return id<y.id;
	}
}a[N];
inline int read()
{
    int x=0,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();
    }
    return x*f;
}
int main()
{
	t=read();
	while(t--)
	{
		k=read();
		int len=0;
		for(int i=1;i<=k;i++){
			vis[i]=0;
			x=read();
			while(x--){
				a[++len].v=read();
				a[len].id=i;
			}
		}
		sort(a+1,a+len+1);
		
		cnt=0;
		mind=2e9;
		
		for(l=1,r=0;l<=len;l++){
			while(cnt<k&&r<len){
				r++;
				if(++vis[a[r].id]==1) cnt++;
			}
			if(cnt<k)break;
			mind=min(mind,a[r].v-a[l].v);
			--vis[a[l].id];
			if(vis[a[l].id]==0)cnt--;
		}printf("%d\n",mind);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值