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 5 slices 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 7 slices.

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

题意:有n个橘子,然后每个有ai片,分给m个人,每个橘子能再分,如果当前ai为偶数对半分,奇数分成两份相差一个,然后问分给m个人后分得最少的人能分得的最大值是多少。

思路:二分+记忆化;

二分答案mid,然后判断当前的是否可以分成m个大于等于mid的值,判断的时候用记忆化搜索复杂度N*log(N)。

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<math.h>
6 #include<string.h>
7 #include<map>
8 typedef long long LL;
9 int plice[1000005];
10 bool check(LL mid);
11 int dfs(int nc,int mid);
12 LL n,m;
13 using namespace std;
14 int ac[10000001];
15 int t = 0;
16 int vis[10000001];
17 const int BufferSize=1<<16;
18 char buffer[BufferSize],*head,*tail;
19 inline char Getchar() {
20 if(head==tail) {
21 int l=fread(buffer,1,BufferSize,stdin);
22 tail=(head=buffer)+l;
23 }
24 return *head++;
25 }
26 inline int read() {
27 int x=0,f=1;char c=Getchar();
28 for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
29 for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
30 return x*f;
31 }
32 int main(void)
33 {
34 //freopen("in.txt","r",stdin);
35 //freopen("out.txt","w",stdout);
36
37 while(scanf("%lld %lld",&n,&m)!=EOF)
38 {
39 LL sum = 0;
40 t = 1;LL maxx = 0;
41 for(int i = 0; i < n; i++)
42 {
43 plice[i] = read();
44 sum += plice[i];
45 maxx = max((LL)plice[i],maxx);
46 }
47 if(sum < m)printf("-1\n");
48 else
49 { sort(plice,plice+n);
50 LL l = 1;
51 LL r = maxx;
52 LL id = -1;
53 while(l <= r)
54 {
55 LL mid = (l + r)/2;
56 if(check(mid))
57 l = mid+1,id = mid;
58 else r = mid-1;
59 t++;
60 }
61 printf("%lld\n",id);
62 }
63 }
64 return 0;
65 }
66 bool check(LL mid)
67 {
68 LL cn = 0;
69 int i;
70 for(i = 0; i < n; i++)
71 {
72 if(plice[i] == mid)
73 {
74 cn++;
75 vis[mid] = t;
76 ac[mid] = 1;
77 }
78 else if(plice[i] > mid)
79 {
80 cn+=(LL)dfs(plice[i],mid);
81 }
82 }
83 if(cn >= m)return true;
84 else return false;
85 }
86 int dfs(int nc,int mid)
87 {
88 if(t == vis[nc])return ac[nc];
89 vis[nc] = t;
90 if(nc < mid)return ac[nc] = 0;
91 if(nc%2)
92 { if(nc/2 < mid)return ac[nc] = 1;
93
94 return ac[nc] = dfs(nc/2,mid)+dfs(nc/2+1,mid);
95 }
96 else
97 {if(nc/2 < mid) return ac[nc] = 1;
98 return ac[nc] = (LL)2*dfs(nc/2,mid);}
99 }
100 //

E. Santa Claus and Tangerines的更多相关文章

  1. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. Santa Claus and Tangerines

    Santa Claus and Tangerines 题目链接:http://codeforces.com/contest/752/problem/E 二分 显然直接求答案并不是很容易,于是我们将其转 ...

  4. 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 standa ...

  5. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

  6. E. Santa Claus and Tangerines 二分答案 + 记忆化搜索

    http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...

  7. CodeForces - 748E Santa Claus and Tangerines(二分)

    题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...

  8. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  9. Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

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

随机推荐

  1. 修复UE4编辑器,ClearLog操作导致的崩溃

    UE4 4.24.3版本,编辑器Output Log窗口中,右键--Clear Log操作很大概率会导致编辑器奔溃:解决办法: 相关文件: Engine\Source\Developer\Output ...

  2. 工作学习2-gcc升级引发的崩溃

    分享一下调查gcc 8.0下,函数漏写返回值崩溃问题,调查记录. 现在新的硬件,基本操作系统都是redhat 8.0,升级后测试时,发现了一个崩溃问题,记录一下. ================== ...

  3. 初学者如何吃透一个Java项目

    不少初学者朋友在学习Java过程中,会对着视频敲Java项目,其中遇到的BUG还能解决,但就是每次敲完一个项目,就感觉很空虚,项目里面的知识点感觉懂了但又好像没懂 这些朋友应该怎样才能掌握一个项目所用 ...

  4. Docker环境中部署Prometheus及node-exporter监控主机资源

    前提条件 已部署docker 已部署grafana 需要开放 3000 9100 和 9090 端口 启动node-exporter docker run --name node-exporter - ...

  5. Elasticsearch【基础入门】

    目录 一.操作index 1.查看index 2.增加index 3.删除index 二.操作index 1.新增document 2.查询type 全部数据 3.查找指定 id 的 document ...

  6. Rest使用get还是post

    1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...

  7. [学习总结]2、android中的VelocityTracker(获得速率用的类)

    参考资料:http://blog.jrj.com.cn/4586793646,5298605a.html 感谢这位兄弟! android.view.VelocityTracker主要用跟踪触摸屏事件( ...

  8. zabbix之修改中文

    #在zabbix服务器安装中文名包 root@ubuntu:~# sudo apt-get install language-pack-zh* #:修改环境变量 root@ubuntu:~# sudo ...

  9. Spring Boot对日志的控制

    一.logback日志技术介绍 Spring Boot中使用的日志技术为logback.其与Log4J都出自同一人,性能要优于Log4J,是Log4J的替代者. 在Spring Boot中若要使用lo ...

  10. Java SPI机制,你了解过吗?

    Life moves pretty fast,if you don't stop and look around once in a while,you will miss it 为什么需要SPI? ...