跳到正文

题解

P11328 [NOISG 2022 Finals] Gym Badges

Harekaze约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年2月16日
更新
2026年2月16日
篇幅
约 2 分钟
题目
P11328

版本

  1. v1当前

1. 题目分析

主要分析一下如何排序。

考虑邻项交换。我们设现在我们的等级为 WW。对于任意的两个比赛 ii 与比赛 jj,我们假设 ii 在前面会比 jj 在前面更优,那么一定有:

  • W+Xi≤LjW+X_i\leq L_j

  • W+Xj>LiW+X_j>L_i

上下相加得:

  • W+Xi+Li<W+Xj+LjW+X_i+L_i<W+X_j+L_j

于是就:

  • Xi+Li<Xj+LjX_i+L_i<X_j+L_j

这样我们就得到了排序方法,也就是按 X+LX+L 从小到大来排序会更优。

接下来就很平凡了,直接反悔贪心即可。

2. 代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
struct node{int x,l;}a[N];
inline bool cmp(node a,node b){return a.x+a.l<b.x+b.l;}
int main()
{
	int n;
	long long now=0;
	scanf("%d",&n);
	priority_queue<int>s;
	for(int i=1;i<=n;i++)scanf("%d",&a[i].x);
	for(int i=1;i<=n;i++)scanf("%d",&a[i].l);
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++)
	{
		if(now<=a[i].l)
		{
			now+=a[i].x;
			s.push(a[i].x);
		}
		else if(!s.empty())
		{
			int t=s.top();
			if(a[i].x<t)
			{
				s.pop();
				now+=a[i].x-t;
				s.push(a[i].x);
			}
		}
	}
	printf("%d\n",s.size());
	return 0;
}
原文