题意
JB非常喜欢糖果。
有一天,他发现了一台糖果机,里面有
N里面有糖果。看完机器的说明书后,他知道他可以选择一个子集
N糖果。每颗糖果都有一个甜味价值。JB 选择子集后,假设所选糖果的平均甜度值为
X,所有甜度值严格大于的糖果
X将属于 JB。JB做出选择后,机器就会消失,所以JB只有一次做出选择的机会。
JB不在乎糖果有多甜,所以他只想做出选择,以最大限度地增加他将获得的糖果数量。JB一直对糖果着迷,无法思考,所以他需要你帮助他。
**思路 **
假设最终选择的集合的平均数不超过 k。
为使平均数不超过 k,应将 ≤ k 的数全部选入,然后贪心选
择 > k 的部分中最小的若干个数。那么
因此将 N 个数从小到大排序后,最优解一定是一个前缀。
然后遍历搜索每一个前缀能够拿到的糖果的最大值,搜索部分就用二分来实现。
代码:
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
ll p[N];
void slove( )
{
int t;
sc_int(n);
for(int i =1;i<=n;i++)
sc_ll(s[i]);
sort(s+1,s+1+n);
for(int i =1;i<=n;i++){
s[i]=s[i-1]+s[i];
p[i]=s[i]/i;//平均值
}
ll res=0;
for(int i=1;i<=n;i++)
{
ll l =1,r=i,mid;
if(s[r]-s[r-1]<=p[i])continue;//如果最大值也小于等于平均值就不进行搜索
while(r>l)
{
mid=l+r+1>>1;
if(s[mid]-s[mid-1]>p[i])r=mid-1;
else l=mid;
}
res=max(res,i-l);
}
cout<<res;
cout<<endl;
}
int main()
{
int t;
// sc_int(t);
// while(t--)
slove();
return 0;
}