Simple Set Problem
从所给的每个数组中选一个数组成新数组,再用新数组的最大值减去最小值,使得差最小
尺取法:
尺取法介绍_nefu_0iq的博客-CSDN博客
线性结构——尺取法_尺取法时间复杂度_Alex_McAvoy的博客-CSDN博客
一维数组或序列中寻找满足特定条件的子区间的方法。通过设置两个指针,即"尺"和"取",来动态调整区间范围,从而快速地找到满足条件的子区间。
通常适用于需要在连续子区间中寻找最优解或满足一定条件的问题,例如求最长连续递增子数组
*单调序列
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;
}