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 ...
随机推荐
- 日志分析工具-ApexSQL介绍
原文:日志分析工具-ApexSQL介绍 使用场景:业务数据异常变化,通过代码分析不出来的时候,迫不得已需要通过日志来分析 下载地址:http://www.apexsql.com/Download.as ...
- Android的FrameLayout使用要注意的问题
帧布局FrameLayout在Android的五大布局中是最简单的布局方式,在需要布局中的控件有重叠的情况下才使用. 今天Shamoo使用FrameLayout的时候,发现布局里面的控件设置layou ...
- 基于MEF的插件框架之总体设计
基于MEF的插件框架之总体设计 1.MEF框架简介 MEF的全称是Managed Extensibility Framework(MEF),其是.net4.0的组成部分,在3.5上也可以使用.熟悉ja ...
- ASP.NET MVC + EF 利用存储过程读取大数据
ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...
- WebService支持多平台上传文件的实现
WebService支持多平台上传文件的实现 要使用网站上传文件,在ASP.NET的范畴,我基本上能想到的有两类,一类是通过HTTP POST请求获得文件信息,另外一类是通过WebService或 ...
- C#单元测试工具包:MvcContrib
C#单元测试工具包:MvcContrib http://t.cn/hE67d https://mvccontrib.codeplex.com/documentation MVC Contrib Doc ...
- 【转】几点 iOS 开发技巧
[译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...
- UNIX系统接口
UNIX系统接口 8.1 文件描述符 UNIX操作系统中,所有的外围设备(包括键盘和显示器)都被看作是文件系统中的文件.系统通过文件描述符来标识文件:标准输入为0,标准输出为1,标准错误为2. 当程序 ...
- 使用PetaPoco结合注入实现业务级事务
使用PetaPoco结合注入实现业务级事务 PetaPoco是一个轻量级ORM,我的MVC项目中使用它结合Repository模式,依靠Unity的生命周期管理对象,保证请求/线程级别的数据上下文 ...
- Memcache Slab Eviction 功能测试
Memcache Slab Eviction 功能测试 1 功能简述 1.1 Automove功能背景 由于memcache的内存分配是基于slab的,每个1M的page内只能存放对应slab大小范围 ...