题目链接:点击查看
题目大意:给出 n 天,每天可以有数件衣服可以选择,但每天只能选择一件衣服穿,每件衣服都有权值,现在需要挑出 m 天的衣服,使得最大值与最小值之差最小
题目分析:比赛时为了恰烂分用了群友不小心说漏嘴的假算法过的(我有罪)
赛后看了题解才恍然大悟,这不就是一个裸的尺取,将所有的衣服权值排序,然后枚举左端点,尺取右端点就好了,尺取的目标是使得区间内存在着恰好 m 件衣服(因为已经排过序了,显然右端点越小越好),那么答案就是维护一下 node[ r ] - node[ l ] 的最小值了
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+100;
vector<pair<int,int>>node;
int cnt[N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
while(k--)
{
int num;
scanf("%d",&num);
node.emplace_back(num,i);
}
}
sort(node.begin(),node.end());
int r=0,now=0,ans=inf;
for(int l=0;l<node.size();l++)
{
while(r<node.size()&&now<m)
{
if(cnt[node[r].second]==0)
now++;
cnt[node[r].second]++;
r++;
}
if(now==m)
ans=min(ans,node[r-1].first-node[l].first);
cnt[node[l].second]--;
if(cnt[node[l].second]==0)
now--;
}
printf("%d\n",ans);
return 0;
}