• 2020牛客暑期多校训练营(第九场)F


    Description

    (n) 种物品,每种物品有若干个,权值不同。要选出 (m) 种物品各 (1) 个使得其权值极差最小。

    Solution

    ((id,val))(val) 排序后,双指针即可,保证扫描区间内始终至少有 (m) 种物品即可。

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long 
    const int N = 2000005;
    
    struct Node 
    {
        int id;
        int val;
        bool operator < (const Node& b)
        {
            return val < b.val;
        }
    } a[N];
    
    int t1,t2,t3,n,m,ind,b[N];
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>t1;
            while(t1--)
            {
                cin>>t2;
                a[++ind]={i,t2};
            }
        }
        sort(a+1,a+ind+1);
        int pos=0,cnt=0,ans=1e9;
        for(int i=1;i<=ind;i++)
        {
            while(pos<ind && cnt<m)
            {
                ++pos;
                if(b[a[pos].id]==0) ++cnt;
                b[a[pos].id]++;
            }
            if(cnt>=m)
            {
                ans=min(ans,a[pos].val-a[i].val);
            }
            b[a[i].id]--;
            if(b[a[i].id]==0) --cnt;
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    python 面向对象(三大特性)
    python 发红包
    python 计算器
    python 模块和包
    python 异常处理
    python 序列化模块
    python 常用模块
    esriSRGeoCS3Type Constants
    esriSRGeoCS2Type Constants
    esriSRGeoCSType Constants
  • 原文地址:https://www.cnblogs.com/mollnn/p/13769867.html
Copyright © 2020-2023  润新知