题目
A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.
Output
In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.
Examples
Input
5 3 6 3 4 0 2
Output
3 4
Input
3 4 5 5 5
Output
3 5
Input
5 3 3 1 2 2 1
Output
4 2
题意
给你一个长度为n的整数序列,你最多有k次操作(k次操作可以不完全用上),每次可以将数组中的一个数加1(一个数字可以多次操作),问你经过适当的操作后,数组中出现的次数最多的数字是什么(如果次数相同,输出最小的那个数字)。先输出次数,再输出那个数。
分析
n的范围是1e5,不可能暴力。先排序,把前缀和存起来,然后遍历该数组,用二分思想去找该数组中,在满足条件的情况下,有多少个数能转换成a[ i ]。
代码
#include<iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll a[N],sum[N];
ll cal(ll l,ll r)//计算从l--r都转换成a[r]需要多少
{
ll tmp=sum[r]-sum[l-1];
tmp=a[r]*(r-l+1)-tmp;
return tmp;
}
ll erfen(ll s,ll k)//二分,计算该数组中能转换成ar[s]的数有多少
{
ll l=1,r=s;
while(l<r)
{
ll mid=(l+r)/2;
ll tmp=cal(mid,s);
if(tmp>k)
l=mid+1;
else
r=mid;
}
return s-l+1;
}
int main()
{
ll n,k;
while(~scanf("%lld%lld",&n,&k))
{
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+1,a+n+1);
sum[0]=0;
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
ll count=0,num;
for(ll i=1;i<=n;i++)//因为排序了,所以从小的开始,找到的一定是小的
{
ll tmp=erfen(i,k);
if(count<tmp)
{
count=tmp;
num=a[i];
}
}
printf("%lld %lld\n",count,num);
}
return 0;
}