Codeforces Round #389 Div.2 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 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.
贪心/二分答案
脑洞题
想明白以后其实挺简单的。然而比赛的时候理解错了题意,没做出来……
如果二分答案的话,每次二分之后暴力统计一下是否可行,好像可以跑过去。
如果贪心的话:
大概一看代码就懂了。先找出最小的一定可行的解,然后将从大到小for循环,把大橘子不断分成两半,同时检查是否可以更新答案。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
int n,k;
int ans=;
LL a[mxn];
int main(){
int i,j;
scanf("%d%d\n",&n,&k);
LL smm=;
for(i=;i<=n;i++){
scanf("%d",&j);
++a[j];
smm+=j;
}
if(smm<k){printf("-1\n");return ;}
smm=;
for(i=mxn-;i;i--){
smm+=a[i];
if(smm>=k){ans=i;break;}
//一定可行的最小答案
}
for(i=mxn-;i>;i--){
if(i/<ans)break;
a[i/]+=a[i];
a[i-i/]+=a[i];
smm+=a[i];
a[i]=;
while(smm-a[ans]>=k){
smm-=a[ans];
ans++;
}
}
printf("%d\n",ans);
return ;
}
Codeforces Round #389 Div.2 E. Santa Claus and Tangerines的更多相关文章
- 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 ...
- Codeforces Round #389 Div.2 C. Santa Claus and Robot
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 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 ...
- 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, Rated, Based on Technocup 2017 - Elimination Round 3) C
Description Santa Claus has Robot which lives on the infinite grid and can move along its lines. He ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B
Description Santa Claus decided to disassemble his keyboard to clean it. After he returned all the k ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) A
Description Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the f ...
随机推荐
- Oracle 11g Express
Oracle 11g Express 安装和使用教程 使用工具的第一步就是安装工具,配置环境!下面就Oracle 11g Express的安装和简单实用做一简介. 一.下载安装过程 去oracle ...
- HDU1281-棋盘游戏-二分图匹配
先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...
- Qt学习笔记网络(一)
Qt5 移除了QHttp是因为功能重复 用QNetworkAccessManager完全能搞定 新建一个控制台应用程序 看一下QNetworkAccessManager的帮助文档 需要添加Qt + = ...
- gocode+auto-complete搭建emacs的go语言自动补全功能
上篇随笔记录了在emacs中使用go-mode和goflymake搭建了go语言的简单编程环境(推送门),今天来记录一下使用gocode+auto-complete配置emacs中go语言的自动补全功 ...
- 乱码电路(Garbled circuits)
乱码电路(Garbled circuits)是Andrew Yao教授在上世纪80年代发明的一种很聪明的技术.它可以让两个人针对某个算式来计算答案,而不需要知道他们在计算式所输入的数字. 举个例子说, ...
- golang: 把sql结果集以json格式输出
func getJSON(sqlString string) (string, error) { stmt, err := db.Prepare(sqlString) if err != nil { ...
- 【Android 开发】: Android 消息处理机制之一: Handler 与 Message
最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...
- hibernate防止sql注入对参数赋值传参数的例子
来源于:https://my.oschina.net/u/1754093/blog/707083 1.按参数名称绑定 在HQL语句中定义命名参数要用":"开头,形式如下: Quer ...
- 【ASP.NET Identity系列教程(二)】运用ASP.NET Identity
注:本文是[ASP.NET Identity系列教程]的第二篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...