跳到正文

题解

P8897 题解

Gaode_Sean约 1 分钟
原文

这是 2026年5月18日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年5月18日
更新
2026年5月18日
篇幅
约 1 分钟
题目
P8897

版本

  1. v2当前
  2. v1

签到题。

首先对 cc 数组进行排序。

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

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

AC Code

C++
#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;
}