Santa Claus and Tangerines

题目链接:http://codeforces.com/contest/752/problem/E

二分

显然直接求答案并不是很容易,于是我们将其转化为判定性问题:二分解x,验证是否能分成k个x。

于是要点就在于check函数:

由于奇偶的原因,每个ai分出来的并不是2的幂次(比如当ai=11,x=3时,可以将ai分成3部分5,3,3)。

但是稍作思考,可以发现还是与2的幂次有关:

例如,当ai=6:48,x=3时,

ai part1 part2 num
6 3 3 2
       
10 5 5 2
11 5 6 3
12 6 6 4
       
20 10 10 4
21 10 11 5
22 11 11 6
23 11 12 7
24 12 12 8
       
40 20 20 8
41 20 21 9
42 21 21 10
43 21 22 11
44 22 22 12
45 22 23 13
46 23 23 14
47 23 24 15
48 24 24 16

若ai=44,因为40<=ai<=48,故num=16-(48-ai);

若ai=38,因为24<=ai<40,故num=8.

这种做法的时间复杂度为O(n×lgA×lglgA)

代码如下:

 #include<cstdio>
#include<algorithm>
#define N 1000005
using namespace std;
typedef long long ll;
ll n,k,a[N],p[];
void init(){
p[]=;
for(ll i=;i<;++i)
p[i]=p[i-]<<;
}
bool check(ll x){
if(!x)return ;
ll sum=;
for(ll i=;i<n;++i){
ll t=a[i]/x;
ll up=*upper_bound(p,p+,t);
if(up*x-up/<=a[i])sum+=up-(up*x-a[i]);
else sum+=up/;
}
return sum>=k;
}
int main(void){
scanf("%I64d%I64d",&n,&k);
init();
for(ll i=;i<n;++i)scanf("%I64d",a+i);
ll l=,r=,mid;
while(l+<r){
mid=(r-l)/+l;
if(check(mid))l=mid;
else r=mid-;
}
if(check(r))printf("%I64d\n",r);
else if(check(l))printf("%I64d\n",l);
else printf("-1\n");
}

Santa Claus and Tangerines的更多相关文章

  1. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. 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 ...

  3. E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. 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 ...

  5. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

  6. E. Santa Claus and Tangerines 二分答案 + 记忆化搜索

    http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...

  7. CodeForces - 748E Santa Claus and Tangerines(二分)

    题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ubuntu下的词典的安装

    因为从事开发,安装一个词典是很有必要,文中介绍安装openyoudao和stardic两个软件的方法 一.openyoudao的安装 因为是由window转来学ubuntu的,所以总是想安装和wind ...

  2. 如何配置Spring的XML文件及使用

    App.config <?xml version="1.0" encoding="utf-8" ?> <configuration> & ...

  3. 【转】Android的Merge讲解与实例

    原文:http://blog.sina.com.cn/s/blog_62f987620100sf13.html 单独将<merge />标签做个介绍,是因为它在优化UI结构时起到很重要的作 ...

  4. 赠书《JavaScript高级程序设计(第三版)》5本

    本站微博上正在送书<JavaScript高级程序设计>走过路过的不要错过,参与方式,关注本站及简寻网+转发微博:http://weibo.com/1748018491/DoCtp6B8r ...

  5. div高度自适外层div高度随里层div高度自适

    尝试过许多办法 其中一网友的最靠谱就是在外层div样式添加两个标签(不能少) clear:both;  overflow:auto;

  6. SpringMVC格式化显示

    SpringMVC学习系列(7) 之 格式化显示 在系列(6)中我们介绍了如何验证提交的数据的正确性,当数据验证通过后就会被我们保存起来.保存的数据会用于以后的展示,这才是保存的价值.那么在展示的时候 ...

  7. Product Trader(操盘手)

    Product Trader(操盘手) 索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Product Trader 的示例实现. 意图 使客户程序可以通过命名抽象超类和给定规 ...

  8. Extjs树形控件入门

    Extjs树形控件由Ext.tree.TreePanel类定义,控件的名称为TreePanel,TreePanel继承自Panel类,在Extjs中使用树形控件其实很简单. 大家知道要使用Extjs必 ...

  9. SQLServer数据库误删数据找回

    记一次SQLServer数据库误删数据找回 昨天 同事在本机清理数据库表时,连接到了生产机,误删了二十几张表,幸好是晚上加班的时候删除的,生产机上当时是一天一备份,还原备份是最后的策略,最关键的还是要 ...

  10. PLAN : 入门题目 ( update )

    更新后 step 1 : A07, A11, A12,A14,A15,A18,A22,A24,A25,A26 A27,A29,A31,A32,A34,A59,A66,A69,A84,B24 B45,B ...