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 ...
随机推荐
- MVC 用扩展方法执行自定义视图,替代 UIHint
MVC 用扩展方法执行自定义视图,替代 UIHint 项目中用了 Bootstrap , 这样就不用写太多的CSS了,省去很多事情.但是这个业务系统需要输入的地方很多,每个表都有100多个字段,每个页 ...
- solr的安装与配置
solr的安装与配置 不久之前开发了一个项目,需要用到solr,因为所以在开始再网上查找资料,但是发现大部分的资料都是很片面的,要么就是只讲解solr如何安装的,要么就是只讲解solr的某一个部分的, ...
- SQL Server 数据库定时自动备份
原文:SQL Server 数据库定时自动备份 SQL Server 数据库定时自动备份——每天定时备份,保留前8天的备份 利用SQL Server代理新建作业来定期备份 1)在数据库库的[SQL S ...
- 无线连接手机进行Android测试
当每天走到哪都要拿一根数据线进行项目测试的时候,总是有一些焦急和烦躁的,如果能够无线连接测试就在好不过了. 这样不再是什么难事了,只需要几步走: 在进行无线连接测试的过程中,你的手机必须root了,这 ...
- C语言中的内存对齐
最近看了好多,也编了好多C语言的浩强哥书后的题,总觉的很不爽,真的真的好怀念linux驱动的代码,好怀念那下划线,那结构体,虽然自己还很菜. 同时看了一遍陈正冲老师的C语言深度剖析,收益很多,又把唐老 ...
- Sqoop自定义多字节列分隔符
Sqoop提供的--fields-terminated-by选项可以支持指定自定义的分隔符,但是它只支持单字节的分隔符,对于我们特殊的需求:希望使用双字节的“|!”,默认的是不支持的. Sqoop在进 ...
- Model 验证
[ASP.NET MVC 小牛之路]16 - Model 验证 上一篇博文 [ASP.NET MVC 小牛之路]15 - Model Binding 中讲了MVC在Model Binding过程中如何 ...
- 温故而知新之java的collection framwork
经常用到的List,Map等这些数据结构,都是来自于java的util包下,而java对于其整体设计,简称为collection framwork.(ps.其实,Map接口并不继承自collectio ...
- IOS 跳转到系统的url
About — prefs:root=General&path=AboutAccessibility — prefs:root=General&path=ACCESSIBILITYAi ...
- jsp中获取当前文件路径
<% String path1 = application.getRealPath(request.getRequestURI()); //当前请求的JSP文件的物理路径 String path ...