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 ...
随机推荐
- AR 不同 继承映射的问题总结
在使用AR(Nhibernate) 做ORM时,使用类的继承体系时,它有不同的映射方式,解决的问题不同,带来的问题差异也很大. 1.所有数据 存储在一张表,不同的类使用 DiscriminatorCo ...
- (转)无法打开C盘,提示"本次操作由于这台计算机的限制被取消,请与你的管理员联系”
今天下了一个网吧用的工具一不小心把系统给限制了好多双击打开C的时候就出现本次操作由于这台计算机的限制被取消,请与你的管理员联系下载一个精锐网吧辅助工具解除了一下现在但是还是不能双击 于是就在网上找帮啊 ...
- Qt学习笔记 线程(一)
Qt中的线程是与平台无关的 QThread 提供了创建一个新线程的方法 新建一个线程,继承QThread并重写它的run()当调用 start()函数时会调用重载的run()函数 例: #ifndef ...
- HoloLens开发手记 - Unity之Recommended settings 推荐设置
Unity提供了大量的设置选项来满足全平台的配置,对于HoloLens,Unity可以通过切换一些特定的设置来启用HoloLens特定的行为. Holographic splash screen 闪屏 ...
- JQuery 图片略缩与弹出预览 jqthumb fancybox
弹出框插件-FANCYBOXhttp://www.jq22.com/jquery-info28 jqthumb.js缩略图插件 http://www.ijquery.cn/?p=798
- JS 问题集锦
[1]js页面跳转 和 js打开新窗口方法 第一种: <script language="javascript" type="text/javascript&quo ...
- MD5加密与验证
package com.study; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException ...
- 发短信的简单实现——C#版
为了验证操作人的身份,界面中通常会有获取验证码的功能.及点击获取验证码就会往你输入的手机号里面发送一条短信进行验证. 最近公司给我的任务中也包含这个功能,那么接下来就让我讲解下. ---------- ...
- 调用天气Api实现天气查询
上面是简单截图: 前台代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...
- XSS attack
<html> <form action="" method="post"> <input type="text" ...