P8897 题解

· · 题解

签到题。

首先对 c 数组进行排序。

注意到 1 \le c_i \le 10^6,不妨考虑直接枚举收取的学费(单价)。设收取的学费为 x 。则二分出有多少奶牛愿意支付的最高金额(即 c_i)大于等于 x,设为 cnt。最后一步,我们用 x \times cnt 与答案进行比较,并更新答案。

时间复杂度为 \mathcal{O}((\max{c_i}) \log n),空间复杂度为 \mathcal{O}(n)。

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int n,p,x;
ll a[N],ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    sort(a+1,a+1+n);
    for(ll i=1;i<=a[n];i++)
    {
        x=lower_bound(a+1,a+1+n,i)-a;
        if(ll(ll(n-x+1)*i)>ans) ans=ll(ll(n-x+1)*i),p=i;
    }
    printf("%lld %d",ans,p);
    return 0;
}