F-Groundhog Looking Dowdy2020牛客暑期多校训练营(第九场)
链接:https://ac.nowcoder.com/acm/contest/5674/F
来源:牛客网
Groundhog finds that Apple seems to be less intimate than before.
He is very distressed for this.After pondering for a long time, Groundhog finds that he looks too dowdy.So, Groundhog decided to improve a little.
Because Groundhog is lazy, he only lists the clothes that can be worn for the next {n}n days. On the i-th day, the j-th
clothes have a dowdiness a(i,j).On each day,he will choose one of the clothes and wear it.
And Groundhog should choose {m}m days from {n}n days to go out with Apple.
Groundhog wants to know the minimum difference between the maximum dowdiness and the minimum dowdiness in {m}m days when Groundhog’s choice is optimal.
Simplified: You should choose n clothes for each day and then choose m clothes from those n clothes, and the problem is to calculate the minimum difference between the maximum dowdiness and the minimum dowdiness.
输入
4 3
1 3
2 8 6
1 2
3 1 7 5
输出
2
说明
Apple will pay attention to Groundhog’s clothes on day 1, 3, and 4
,Groundhog will wear clothes with dowdiness of 3, 2, and 1 on day 1,
3, and 4,and the difference is 2.
题意:
有n天,每天土拨鼠有一些衣服可以穿。
其中mm天他要出去见Apple,因此他希望穿一些更特别的衣服。每件衣服都有一个dowdiness不时髦度。
现在土拨鼠希望对于任意的m天每天找到一件衣服使得选出的这些衣服中dowdiness的最大值和最小值的差的最小值。
题解:
知识点:尺取法
用滑动模块去做,把全部衣服的dowdiness值从小到大排序,然后用尺取法从小到大去找最小的模块,条件是要满足m天。
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<vector>
#include<time.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#define ll __int128
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0),eps=1e-8;
struct node
{
int id;
int val;
} num[2000005];
bool cmp(node a,node b)
{
return a.val<b.val;
}
int vis[2000005];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
int cnt=0;
for(int i=0; i<n; i++)
{
int k;
scanf("%d",&k);
while(k--)
{
int xx;
scanf("%d",&xx);
num[cnt].id=i;
num[cnt++].val=xx;
}
}
sort(num,num+cnt,cmp);
int l=0,r=0;
int sum=0,minn=INF;
while(r<=cnt-1)
{
if(++vis[num[r].id]==1)
sum++;
while(sum==m)
{
minn=min(minn,num[r].val-num[l].val);
if(--vis[num[l++].id]==0)
sum--;
}
r++;
}
printf("%d\n",minn);
return 0;
}