codeforces604B
More Cowbell
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.
Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.
Input
The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively.
The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.
Output
Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.
Examples
2 1
2 5
7
4 3
2 3 5 9
9
3 2
3 5 7
8
Note
In the first sample, Kevin must pack his two cowbells into the same box.
In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.
In the third sample, the optimal solution is {3, 5} and {7}.
sol:很显然答案是可以二分的,难点在于判断当前答案是否可行,一种较为容易想到的贪心,尽量用一个最大的配上一个最小的,易知一定是最优的
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N];
inline bool Judge(int mid)
{
int l=,r=n,cnt=;
while(l<=r)
{
if(a[l]+a[r]<=mid)
{
cnt++; l++; r--;
}
else
{
cnt++; r--;
}
}
return (cnt<=m)?:;
}
int main()
{
int i;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
sort(a+,a+n+);
int l=a[n],r=;
while(l<=r)
{
int mid=(l+r)>>;
if(Judge(mid)) r=mid-;
else l=mid+;
}
Wl(l);
return ;
}
/*
input
2 1
2 5
output
7 input
4 3
2 3 5 9
output
9 input
3 2
3 5 7
output
8
*/
codeforces604B的更多相关文章
随机推荐
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- Lodop 打印控件
1.下载 2.使用 一 下载安装控件 官网下载地址:http://www.lodop.net/download.html 参考:http://www.c-lodop.com/demolist/Prin ...
- C语言的基本数据类型
代码如下: // text.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" int main() { int a; //告诉编译器,分配 4 ...
- QT 字符串的使用技巧总结
QT 的字符串的 使用的总结. 1.字符串截取函数的使用 QString str; QString csv = "forename,middlename,surname,phone" ...
- 运行Android Studio自带模拟器报:Guest isn't online after 7 second...
今天在运行Android Studio自带的手机模拟器时,出现如下异常情况 : 解决办法: 1.打开Android Virtue Device Manager,点击编辑选项 2.点击show Adva ...
- php中按值传递和按引用传递的一个问题
php中传递变量默认是按照值传递. 简单举个例子: <?php function testArray($arr){// &$arr $arr = array(1,2,3,); } $ar ...
- Ionic App之国际化(2) json数组的处理
在Ionic App值国际化(1)中我们实现了对单个参数的多语言处理,下面开始如何进行数组的处理. 1.在我们的多语言文件中设置要访问的json数组,en.json和zh.json,此处就以en.js ...
- Ionic 部分手机升级不成功的问题
Android端的手机App发布之后的一段时间,用户反馈App无法升级的情况. 原因分析: 对代码进行错误在线,提示是FileNOTFindException错误,确定是下载的时候保存的目的路径 不存 ...
- Spring Aop: 关于继承和execution target this @annotation
1.多态 target指通过这个对象调用的方法 (匹配标识对象的所有方法) getMethod() this指调用这个对象的方法 (匹配标识对象实现的方法) getDeclaredMethod( ...
- retinex图像增强算法的研究
图像增强方面我共研究了Retinex.暗通道去雾.ACE等算法.其实,它们都是共通的.甚至可以说,Retinex和暗通道去雾就是同一个算法的两个不同视角,而ACE算法又是将Retinex和灰度世界等白 ...