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.但是按照平均分的话, ...
随机推荐
- 也谈微信小程序
小程序是一种不需要下载安装即可使用的应用,它实现了应用"触手可及"的梦想,用户扫一扫或者搜一下即可打开应用.也体现了"用完即走"的理念,用户不关心是否安装太 ...
- Swift 写纯洁的TableviewCell
let initIdentifier = "员工" var cell = tableView.dequeueReusableCell(withIdentifier: initIde ...
- BufferedReader与Scanner的区别
在Java中,我们都知道Java的标准输入串是System.in.但是我们却很少在Java中看到谁使用它,这是因为我们平时输入的都是一个字符串或者是一个数字等等.而System.in提供的read方法 ...
- Windows Phone 七、XML序列化
DataContractSerializer对象 public class Person { public int Id { get; set; } public string Name { get; ...
- Android Studio 使用Lambda
1,昨天在使用RxJava的时候,调用map.filter之类的方法要创建挺多的匿名内部类,所以我们打算试用一下Lambda让我们的代码更有阅读新性,下看一下我们的对比 在使用之前我们代码是这样的 O ...
- Html5shiv
说明编辑 越来越多的站点开始使用 HTML5 标签.但情况是还有很多人在使用IE6,IE7,IE8.为了让所有网站浏览者都能正常的访问网站, 2解决方案编辑 有下面两个: 为网站创建多套模板,通过程序 ...
- springMVC使用@ResponseBody返回json
json格式:{"totle":22,"rows":"sss"} map格式:{totle=22, rows=sss} 一.springMV ...
- Mountains(CVTE面试题)解题报告
题目大意: 用一个数组代表群山的高度.高度大的地方代表山峰,小的地方代表山谷.山谷可以容水.假设有一天下了大雨,求群山中总共可以容纳多少水? 如图所示情况,a代表该数组,总共可以容纳5个水. 解题思路 ...
- 在ie与火狐的兼容性
1.在火狐下 document.getElementById("id").textContent可以获取当前获取id下的内容 而在ie下则为:document.getElem ...
- JVM内存管理
前几天公司的郑大晔校上,XXX同事做了JVM的Session,于是趁端午节放假的功夫,研究了一些JVM相关的知识. 在Java生态系统中,JVM占据至关重要的作用,就像一个适配器,它向编程语言(主要是 ...