2021牛客寒假算法基础集训营3 B. 内卷

链接

https://ac.nowcoder.com/acm/contest/9983/B

题意

每个人有 ABCDE 五个等级的预期分数,学校要求得到等级 A 的人不超过 k k k 个。

给每个人安排一个预期分数,求他们的预期分数最大值和最小值之差最小为多少。

思路

5 n 5n 5n 个分数从小到大排序。

保证尺取区间内有 n n n 个不同的同学以及取等第 A 的学生不大于 k k k 个即可。

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=1e5+5;
int n,k,tot[N];
bool st[N];
VPII o;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>k;
    for(int i=0;i<n;i++) {
        int a,b,c,d,e;
        cin>>a>>b>>c>>d>>e;
        o.EB(a,i);
        o.EB(b,i+n);
        o.EB(c,i+n);
        o.EB(d,i+n);
        o.EB(e,i+n);
    }
    sort(ALL(o));
    o.resize(unique(ALL(o))-o.begin());
    int l=0,r=-1,res=0x3f3f3f3f,sz=0,cnt=0;
    while(r<SZ(o)) {
        if(sz<n) {
            ++r;
            if(++tot[o[r].SE%n]==1) ++sz;
            if(o[r].SE<n) {
                st[o[r].SE]=true;
                if(tot[o[r].SE]==1) ++cnt;
            }
            else if(st[o[r].SE-n]&&tot[o[r].SE-n]==2) --cnt;
        }
        else {
            if(sz==n&&cnt<=k) res=min(res,o[r].FI-o[l].FI);
            if(--tot[o[l].SE%n]==0) --sz;
            if(o[l].SE<n) {
                st[o[l].SE]=false;
                if(tot[o[l].SE]==0) --cnt;
            }
            else if(st[o[l].SE-n]&&tot[o[l].SE-n]==1) ++cnt;
            ++l;
        }
    }
    cout<<res<<'\n';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值