Santa Claus and Tangerines
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的更多相关文章
- codeforces 748E 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) E. Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- [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.但是按照平均分的话, ...
- 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) 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 D. Santa Claus and a Palindrome
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- js获取当前指定的前几天的日期(如当前时间的前七天的日期)
这里就不多说了,直接贴上代码: <html> <head> <meta http-equiv="Content-Type" content=" ...
- 处理动态SQL语句的参数
原文:处理动态SQL语句的参数 经常对SQL进行开发,写动态的SQL语句,是少之不了的,但是在使用动态语句中,常是因为有动态的参数的出现.参考下面代码示例: 正因为有了标记1的动态条件代码,而让SQL ...
- 通过改变viewport 实现网站自适应
var phoneWidth = parseInt(window.screen.width); var phoneScale = phoneWidth/640; var userAgent = nav ...
- TodoList开发笔记 – PartⅠ
做了一年多的桌面软件,最近开始转向Web方面的开发,既然比较熟悉Net那么首当其冲就是学习ASP.Net,以及HTML.CSS.Javascript. 为了检验这个把星期来的学习成果,着手做了一个To ...
- .NET开发面向对象1
ASP.NET开发,从二层至三层,至面向对象 昨天Insus.NET有写了一篇博文<WEB控件没有什么所谓好不好,而是用得好不好>http://www.cnblogs.com/insus/ ...
- Bootstrap 模态框(也可以说的弹出层)
最近在尝试使用bootstrap的模态框 使用模态框主要要引入一下几个js和css: bootstrap.css jquery.1.9.1.js(这个可以灵活选择) bootstrap.js html ...
- Net 4.0 之 Dynamic 动态类型
Net 4.0 之 Dynamic 动态类型 本文主要旨在与网友分享.Net4.0的Dynamic 对Duck Type 的支持. 一..net4.0主要新特性 .Net4.0在.Net3.5 ...
- hightchart导出图片
通常在使用highchart导出图片pdf等文件时,我们一般直接引入exporting.js即可 执行导出操作则会直接请求highchart服务器,执行生成图片等操作,然后下载到客户端: 但这一切的操 ...
- C#利用Emit反射实现AOP,以及平台化框架封装思路
C#利用Emit反射实现AOP,以及平台化框架封装思路 这是前两天扒的一段动态代理AOP代码,用的Emit反射生成子类来实现代理模式,在这里做个小笔记,然后讨论一下AOP框架的实现思路. 首先是主函数 ...
- .net下将富文本编辑器文本原样读入word文档
关键词:富文本编辑器 生成word 样式 为了解决标题中提出的问题,首选需要了解,在.net环境下读取数据库中的内容动态生成word至少有2种方式,[方式一]一种方式是在项目中添加引用,例如在“添 ...