生活随笔
收集整理的這篇文章主要介紹了
牛客多校9 - Groundhog Looking Dowdy(尺取)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出 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;
}
?
總結
以上是生活随笔為你收集整理的牛客多校9 - Groundhog Looking Dowdy(尺取)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。