Codeforces Round #389 Div.2 E. Santa Claus and Tangerines

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Santa Claus has n tangerines, and the i-th of them consists of exactly ai slices. Santa Claus came to a school which has k pupils. Santa decided to treat them with tangerines.

However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into parts so that no one will be offended. In order to do this, he can divide a tangerine or any existing part into two smaller equal parts. If the number of slices in the part he wants to split is odd, then one of the resulting parts will have one slice more than the other. It's forbidden to divide a part consisting of only one slice.

Santa Claus wants to present to everyone either a whole tangerine or exactly one part of it (that also means that everyone must get a positive number of slices). One or several tangerines or their parts may stay with Santa.

Let bi be the number of slices the i-th pupil has in the end. Let Santa's joy be the minimum among all bi's.

Your task is to find the maximum possible joy Santa can have after he treats everyone with tangerines (or their parts).

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 2·109) denoting the number of tangerines and the number of pupils, respectively.

The second line consists of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 107), where ai stands for the number of slices the i-th tangerine consists of.

Output

If there's no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have.

Examples
input
3 2
5 9 3
output
5
input
2 4
12 14
output
6
input
2 3
1 1
output
-1
Note

In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil.

In the second example Santa should divide both tangerines, so that he'll be able to present two parts with 6 slices and two parts with 7slices.

In the third example Santa Claus can't present 2 slices to 3 pupils in such a way that everyone will have anything.

贪心/二分答案

脑洞题

想明白以后其实挺简单的。然而比赛的时候理解错了题意,没做出来……

如果二分答案的话,每次二分之后暴力统计一下是否可行,好像可以跑过去。

如果贪心的话:

  大概一看代码就懂了。先找出最小的一定可行的解,然后将从大到小for循环,把大橘子不断分成两半,同时检查是否可以更新答案。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #define LL long long
 7 using namespace std;
 8 const int mxn=10000100;
 9 int n,k;
10 int ans=1;
11 LL a[mxn];
12 int main(){
13     int i,j;
14     scanf("%d%d
",&n,&k);
15     LL smm=0;
16     for(i=1;i<=n;i++){
17         scanf("%d",&j);
18         ++a[j];
19         smm+=j;
20     }
21     if(smm<k){printf("-1
");return 0;}
22     smm=0;
23     for(i=mxn-1;i;i--){
24         smm+=a[i];
25         if(smm>=k){ans=i;break;}
26         //一定可行的最小答案 
27     }
28     for(i=mxn-1;i>1;i--){
29         if(i/2<ans)break;
30         a[i/2]+=a[i];
31         a[i-i/2]+=a[i];
32         smm+=a[i];
33         a[i]=0;
34         while(smm-a[ans]>=k){
35             smm-=a[ans];
36             ans++;
37         }
38     }
39     printf("%d
",ans);
40     return 0;
41 }
原文地址:https://www.cnblogs.com/SilverNebula/p/6224317.html