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

链接

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

题意

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

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

思路

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

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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;
}