C. To Add or Not to Add
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
Note
In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.
-------------------------------------------editorial---------------------------
One of the main observations, needed to solve this problem, is that the second number in answer always coincides with someone aj. Let's see why it is true. Suppose, the second number of the answer is aj + d for someone j and aj + d ≠ ai for all i. This means, we increased some numbers, which is less than aj, so that they became equal to aj, and then all this numbers and some numbers, which is equal to aj, we increased to aj + d. But if we didn't increase all this numbers to aj + d and remain they equal to aj, we'd perform less operations and the answer would be better.
Due to this fact we can solve problem in a such manner. Sort array in non-decreasing order. Iterate over ai and calculate, what is the maximal number of ai we can obtain. For maximizing first number of answer, we must increase some lesser numbers to ai and perform not greater than k operations. It is obvious that firstly we should increase such aj that ai–aj is minimal. So, if we can solve problem inO(n2), we would iterate j from i to 0 and increase aj to ai, while we could. But the solution must be faster, and we will use binary search. We will brute the number of numbers, which we must do equal to ai. Suppose we fix cnt this value. Now we have to check if we can do cnt numbers equal to ai by not greater than k operations. For doing this, let’s calculate
. If this value not greater than k, we can do it. For calculating sum quickly, we can save prefix sums and than si - cnt + 1, i = si–si–cnt. Finally we solved this problem in O(n·logn).
--------------------------------------------------code------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli arr[1000000];
vector<lli>v;
lli fs[1000000];
int main()
{
int n;
lli k ;
cin>>n>>k;
for(int i=0;i<n;i++)
{
lli a;
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
for(int i=1;i<=n;i++)
{
fs[i]=fs[i-1]+v[i-1];
// cout<<fs[i]<<" ";
}
int maxi=0,len=0;
lli val=0;
for(int i=1;i<=n;i++)
{
int l=1,r=i;
len=0;
int mx=32;
while(mx--)
{
if(l>r) break;
int mid=(l+r)/2;
if((fs[i]-fs[mid-1]+k)>=(v[i-1]*(i-mid+1)))
{
r=mid;
len=i-mid+1;
}
else
{
l=mid+1;
}
}
if(len>maxi)
{
maxi=len;
val=v[i-1];
}
}
cout<<maxi<<" "<<val<<endl;
}
No comments:
Post a Comment