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. 前端1 — HTML — 更新完毕

    1.首先来了解一个东西 -- W3C标准( 全称是:World Wide Web Consortium ) 万维网联盟(外语缩写:W3C)标准不是某一个标准,而是一系列标准的集合 -- 这个其实每天都 ...

  2. mysql数据操作语言DML

    插入insert 插入方式1 语法: insert into 表名(列名,....) values(值1,....) 说明: 1.插入的值的类型要与列的类型一致或兼容 2.可以为null的值:①列写了 ...

  3. InnoDB学习(二)之ChangeBuffer

    ChangeBuffer是InnoDB缓存区的一种特殊的数据结构,当用户执行SQL对非唯一索引进行更改时,如果索引对应的数据页不在缓存中时,InnoDB不会直接加载磁盘数据到缓存数据页中,而是缓存对这 ...

  4. Scala(五)【集合的高级使用】

    目录 一.集合属性 size length contains mkString 二.集合方法 drop.dropRight distinct:去重 head.last:获取第一个.最后一个元素 tai ...

  5. Linux网络管理(一)之配置主机名与域名

    Linux网络管理(一)之配置主机名与域名参考自:[1]修改主机名(/etc/hostname和/etc/hosts区别) https://blog.csdn.net/shmily_lsl/artic ...

  6. Spring Cloud中使用Eureka

    一.创建00-eurekaserver-8000 (1)创建工程 创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即 ...

  7. 解决Spring MVC @ResponseBody出现问号乱码问题

    原因是SpringMVC的@ResponseBody使用的默认处理字符串编码为ISO-8859-1,而我们前台或者客户端的编码一般是UTF-8或者GBK.现将解决方法分享如下! 第一种方法: 对于需要 ...

  8. 使用Zabbix + Python对Mysql监控

    一.背景介绍 随着公司业务的变迁,公司的开发数据库以mysql为主了.mysql服务器层面的监控,例如CPU.内存.硬盘空间等就用zabbix自带的linux模板即可.数据库层面zabbix也自带了一 ...

  9. 模板方法模式(Template Method Pattern)——复杂流程步骤的设计

    模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...

  10. Nginx 架构基础

    1 Nginx请求处理流程 2 Nginx进程结构 3 Nginx进程管理:信号 3.1 Master进程 监控worker进程 CHLD 管理worker进程 接收信号 TERM,INT QUIT ...