codeforces 748E 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 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.
根据题目所求考虑二分最终答案,发现Ai的值域有限所以可以直接用数组记录每个大小的有多少个,然后类似于NOIP2016DAY2T2的那个暴力就可以了(直接扫一遍存储数组)复杂度O(max{ai}*log(max{ai}));
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1000010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,a[maxn],ans,i,maxl,l,r,mid;
llg c[]; llg check(llg x)
{
llg tot=;
for (i=;i<=maxl;i++) c[i]=;
for (i=;i<=n;i++) c[a[i]]++;
for (i=maxl;i>=x;i--)
{
if (i/>=x)
{
if (i%) {c[i/]+=c[i]; c[i/+]+=c[i];}
else {c[i/]+=c[i]*;}
}
else
{
tot+=c[i];
}
}
if (tot>=m) return ;else return ;
} int main()
{
cin>>n>>m;
for (i=;i<=n;i++) { scanf("%I64d",&a[i]); r=max(r,a[i]);}
l=; maxl=r;
while (l<=r)
{
mid=(l+r)/;
if (check(mid)) {l=mid+; ans=mid;}else {r=mid-;}
}
if (ans==) ans--;
cout<<ans;
return ;
}
codeforces 748E Santa Claus and Tangerines的更多相关文章
- 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) 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 二分 显然直接求答案并不是很容易,于是我们将其转 ...
- E. 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 E. Santa Claus and Tangerines
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- [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.但是按照平均分的话, ...
随机推荐
- sp_helpdb
语法 sp_helpdb [ [ @dbname= ] 'name' ] 参数 [@dbname=] 'name' 是要为其提供信息的数据库名称.name 的数据类型为 sysname,无默认值.如果 ...
- grunt 入门学习
前端工作流,Grunt上手指南 Posted@2013-04-20 7:15 a.m. CategoriesGrunt , javascript 我想先花点时间回忆一下作为一个前端需要做的工作(Lo ...
- Windows XP 新增API函数列表
SetFileShortNameConvertFiberTothreadCreateFiberExDuplicateEncryptionInfoFileEnumGeoInfoProcEnumSyste ...
- SQL中EXISTS的用法
比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FR ...
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- [MacOS] 终端使用ssh时,中文乱码问题处理
终端显示中文问题处理: vim ~/.inputrc 加入以下代码 set meta-flag on set convert-meta off set input-meta on set output ...
- 盒子模型(W3C盒子模型、IE盒子模型)
盒子模型:一个物体在页面中所占据的位置 盒子模型包含以下几种元素: padding:margin:content:border 这是大家都知道的,也是书本上定义说明的,但是在ie的情况下是有点区别的; ...
- EditPlus 3.6 中文版已经发布(2013年12月17日)
新版的EditPlus 3.6修复了多项问题,并新增了函数列表面板.按F2.Ctrl+F2等快捷键可弹出输入提示窗格. 我已同步汉化最新版,请点击右上角的连接下载.
- linux命令:crontab命令(转)
一.crond简介 二.crond服务 三.crontab命令详解 四.使用注意事项 linux系统是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作 ...
- 利用反射+AOP,封装Basehandler
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...