您的位置:首页 > 其它

CodeForces 748E. Santa Claus and Tangerines 题解(递推or二分)

2017-03-11 22:45 423 查看
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.

题意:有n个数分给m个人

每个数可以分成两个新的数 i/ 2 、(i+1)/2 分后的数可以继续分,但不允许分1=1+0

最后分给每个人,求分得的数最小的人分得的数最大能多少。

这题学会了这么往下传递。。数组下标作为那个体积。。。数值代表的是这个体积的数量,一开始无从下手、、、

有两个方法。

第一个方法是二分答案,然后从1e7到二分的值按照i的奇偶性把i的值传给i/2或i/2和

i/2+1,然后计算当前二分的值是否能分给m个人。时间复杂度是n*logn*logn,勉强能过.

参考博客:点击打开链接

第二个方法是倒着直接枚举答案,枚举到i时,把当前值传给i/2和(i+1)/2两个儿子,统计

当前值能分给多少人时需要减去父亲的贡献,i的父亲有i*2-1, i*2, i*2+1, 但是为防止重复

删去父亲贡献,每次只需减去i*2-1, i*2这两个父亲(如只有一个7片的橘子,分成3 4,

枚举到4时删除7 8,到3时删除5 6,不用再删除7)。复杂度是n,快了很多。

参考博客:点击打开链接

二分代码:

因为确定最小的了, 所以在这个区间内 就让数量最多,看能不能达到m个人,如果i/2 i/2+1大于mid,那就把他分开,让更多的人有橘子

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e7;
const int maxm = 1e6;
ll a[maxm], b[maxn], maxx, n, k;
int check(ll x)
{
ll cnt = 0;
for(int i = 1; i <= 10000000; i++)
b[i] = 0;
for(int i = 1; i <= n; i++)  //计算有几个人
b[a[i]]++; //b下标是代表的橘子的体积
for(int i = 10000000; i >= x; i--)
{
if(i/2 >= x)  //如果比最小的x大,就可以分开,让更多人都有橘子
{
if(i&1)
{
b[i/2] += b[i];
b[i/2+1] += b[i];
}
else
b[i/2] += 2*b[i];
}
else
cnt += b[i];
}
return cnt >= k;
}
int main()
{
while(~scanf("%lld%lld", &n, &k))
{
maxx = -1;
for(int i = 1;i <= n; i++)
{
scanf("%lld", &a[i]);
}
ll l = 1, r = 1e7, mid, ans = -1;
while(l <= r)
{
mid = (l+r)/2;
if(check(mid))
{
ans = mid;
l = mid+1;
}
else
r = mid-1;
}
if(ans == -1)
{
puts("-1");
}
else
{
printf("%lld\n", ans);
}
}
return 0;
}
递推代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e6 + 5;
const int maxx = 1e7 + 5;
typedef long long ll;
ll a[maxn], cnt[maxx];
int main()
{
int n, k;
scanf("%d%d", &n, &k);
ll ans = 0;
for(int i = 1; i <= n; i++)
scanf("%lld", &a[i]), cnt[a[i]]++, ans += a[i];
if(ans < k)
{
return puts("-1\n"), 0;
}
ans = 0;
for(int i = maxx; i >= 1; i--)
{
ans += cnt[i];
if(i*2 <= maxx) ans -= cnt[i*2];
if(i*2-1 <= maxx && i != 1) ans -= cnt[i*2-1];
if(ans >= k)
{
printf("%d\n", i);
break;
}
cnt[i/2] += cnt[i];
cnt[(i+1)/2] += cnt[i];
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: