1. 题目分析
主要分析一下如何排序。
考虑邻项交换。我们设现在我们的等级为 。对于任意的两个比赛 与比赛 ,我们假设 在前面会比 在前面更优,那么一定有:
上下相加得:
于是就:
这样我们就得到了排序方法,也就是按 从小到大来排序会更优。
接下来就很平凡了,直接反悔贪心即可。
2. 代码
#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;
}