E. Santa Claus and Tangerines
2 seconds
256 megabytes
standard input
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).
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.
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.
3 2
5 9 3
5
2 4
12 14
6
2 3
1 1
-1
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的更多相关文章
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- Santa Claus and Tangerines
Santa Claus and Tangerines 题目链接:http://codeforces.com/contest/752/problem/E 二分 显然直接求答案并不是很容易,于是我们将其转 ...
- 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 ...
- [CF752E]Santa Claus and Tangerines(二分答案,dp)
题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...
- E. Santa Claus and Tangerines 二分答案 + 记忆化搜索
http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...
- CodeForces - 748E Santa Claus and Tangerines(二分)
题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...
- 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 ...
- 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 ...
随机推荐
- 11.13python第一周周末练习
2.请输出你的基本个人信息 3.结合逻辑判断,写一个不同学生分数,输出良好,优秀,分数不及格 循环输出 字符串的替换. 以什么开头startwith 以什么结尾endwith 列表转为字符串 字符串转 ...
- 63.不同路径II
目录 63.不同路径Ⅱ 题目 题解 63.不同路径Ⅱ 题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动 ...
- ios加载html5 audio标签用js无法自动播放
html5 audio标签在ios 微信浏览器中是无法自动播放的,最近在做一个小的项目遇到这个问题,安卓和pc都是正常的,唯独ios不行,查阅了很多资料,找到了以下方法,也许不是最好用的方法,如果有更 ...
- C++ 之杂记
今天做了一个题,代码不难,但是编译的时候就恼火,老是报错,也不告诉我错哪了.... 之前的代码是这样的,在main函数中调用这个类的构造函数,就一直报错,但是不知道原因,后来加上了const 就好了. ...
- ORACLE CACHE BUFFER CHAINS原理
原理图如下: 一个cache buffer chains 管理多个hash bucket,受隐含参数:_db_block_hash_buckets(控制管理几个hash bucket)
- Dubbo多协议支持
除了Dubbo服务暴露协议Dubbo协议外,Dubbo框架还支持另外8种服务暴露协议:RMI协议.Hessian协议.HTTP协议.WebService协议.Thrift协议.Memcached协议. ...
- 理解inode以及软硬连接,和inode磁盘爆满的解决方案以及文件权限
理解Linux的软硬链接 创建硬链接的命令 [root@centos6 data]#ln /data/f1 /data/f2 [root@centos6 data]#ll -itotal 1613 - ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- Java知识点总结——IO流框架
IO框架 一.流的概念 概念:内存与存储设备之间传输数据的通道. 二.流的分类 按方向分类: 输入流:将<存储设备>中的内容读入到<内存>中 输出流:将<内存>中的 ...
- Java常用类,这一次帮你总结好!
常用类 常用类概述: 内部类 Object类 包装类 数学类 时间类 字符串 String Builder和StringBuffer DecimalFormat 一.内部类 概念:在一个类内部再定义一 ...