xtu summer individual 5 A
To Add or Not to Add
64-bit integer IO format: %I64d Java class name: (Any)
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.
Sample Input
5 3
6 3 4 0 2
3 4
3 4
5 5 5
3 5
5 3
3 1 2 2 1
4 2
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 #define INF 0x3f3f3f3f 11 using namespace std; 12 const int maxn = 100010; 13 int d[maxn],n,k,temp; 14 LL sum[maxn]; 15 int check(int p){ 16 for(int i = p; i <= n; i++){ 17 if((LL)d[i]*p - sum[i]+sum[i-p] <= k) 18 return i; 19 } 20 return -1; 21 } 22 int main(){ 23 int i,lt,rt,mid,num,ans; 24 while(~scanf("%d%d",&n,&k)){ 25 for(i = 1; i <= n; i++) 26 scanf("%d",d+i); 27 sort(d+1,d+n+1); 28 sum[0] = 0; 29 for(i = 1; i <= n; i++) 30 sum[i] = sum[i-1]+d[i]; 31 lt = 1; 32 rt = n; 33 while(lt <= rt){ 34 mid = (lt+rt)>>1; 35 temp = check(mid); 36 if(temp == -1) rt = mid-1; 37 else{ 38 ans = temp; 39 num = mid; 40 lt = mid+1; 41 } 42 } 43 printf("%d %d ",num,d[ans]); 44 } 45 return 0; 46 }