求调
查看原帖
求调
1078846
cute_楼主2024/7/24 09:52
#include<bits/stdc++.h>
using namespace std;
int t[1001];
int n;
int s;
int p;
int l=1;
int r=1;
int type;
int ans=INT_MAX;
struct student{
	int c;
	int h;
}a[1000001];
int cmp(student x,student y){
	return x.h<y.h; 
}
int main(){
	cin>>n;
	cin>>s;
	p=n*s;
	for(int i=1;i<=p;i++){
		cin>>a[i].h;
		a[i].c=ceil(i/s);
	}
	sort(a+1,a+p+1,cmp);
	while(l<=r&&r<=p){
		if(type<n){
			t[a[r].c]++;
			if(t[a[r].c]==1){
				type++;
			}
			r++;
		}
		else{
			ans=min(ans,a[r-1].h-a[l].h);
			t[a[l].c]--;
			if(t[a[l].c]==0){
				type--;
			}
			l++;
		}
	}
	cout<<ans;
}
2024/7/24 09:52
加载中...