1 条题解

  • 0
    @ 2026-5-18 22:16:11

    签到题。

    首先对 cc 数组进行排序。

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

    时间复杂度为 O((maxci)logn)\mathcal{O}((\max{c_i}) \log n),空间复杂度为 O(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;
    } 
    
    • 1

    信息

    ID
    6970
    时间
    2000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    43
    已通过
    20
    上传者