B. More Cowbell

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/604/problem/B

Description

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.

Sample Input

2 1
2 5

Sample Output

7

HINT

题意

给你n个物品,k个盒子,每个盒子最多可以塞进去2个物品,但是塞进去的物品的权值和必须小于盒子的权值

问你盒子的权值最小可以为多少

保证n<=2*k

题解:

二分答案,check的时候,有一个贪心

最小的+最大的这样扔进去,比两个最小的这样扔进去更加优越

代码:

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std; int n,k;
int a[];
int check(int x)
{
for(int i=;i<=n;i++)
if(a[i]>x)return ;
int ans = ;
int st = ,ed = n;
while()
{
if(st>ed)break;
if(a[st]+a[ed]<=x)
{
st++,ed--;
ans++;
}
else
{
ed--;
ans++;
}
}
if(ans<=k)return ;
return ;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int l = ,r = * ;
while(l<=r)
{
int mid = (l+r)/;
if(check(mid))r = mid-;
else l = mid+;
}
printf("%d\n",l);
}

Codeforces Round #334 (Div. 2) B. More Cowbell 二分的更多相关文章

  1. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  2. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  3. Codeforces Round #334 (Div. 2)

    水 A - Uncowed Forces #include <bits/stdc++.h> using namespace std; typedef long long ll; const ...

  4. Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数

    D. Moodular Arithmetic Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/60 ...

  5. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  6. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  7. Codeforces Round #334 (Div. 1) C. Lieges of Legendre

    Lieges of Legendre 题意:有n堆牛,每堆有ai头牛.两个人玩一个游戏,游戏规则为: <1>从任意一个非空的堆中移走一头牛: <2>将偶数堆2*x变成k堆,每堆 ...

  8. Codeforces Round #334 (Div. 1) B. Moodular Arithmetic

    B - Moodular Arithmetic 题目大意:题意:告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f( ...

  9. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

随机推荐

  1. 【转】cocos2d-x Lua

    Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...

  2. Android 快速开发框架XUtils

    转载自:http://www.apkbus.com/forum.php?mod=viewthread&tid=241060&highlight=xUtils 最近搜了一些框架供初学者学 ...

  3. 【LeetCode 201】Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  4. 将iOS中Safari 的默认搜索引擎由google.cn改为google.com的方法

    众所周知虽然Google大部分的业务已经迁出中国大陆,访问Google的中国站点只会出现一个投影网站,但是很长一段时间里如果想要访问Google仍然能跳转到google.com.hk这个香港的节点,这 ...

  5. sudo source /etc/profile 提示找不到source命令

    今天修改了/etc/profile,需要用source一下,于是写入命令 sudo source /etc/profile 提示找不到source命令 改成    sudo . /etc/profil ...

  6. 纯CSS3实现宽屏二级下拉菜单

    今天我们要来分享一款基于纯CSS3的宽屏二级下拉菜单,这款菜单的子菜单在展开的时候是很宽敞的,菜单项中可以自定义格式的内容,非常实用,也很大气.由于是用纯CSS3实现,所以这款下拉菜单不用运行Java ...

  7. PyBayes的安装和使用

    PyBayes 主页 文档 PyBayes is an object-oriented Python library for recursive Bayesian estimation (Bayesi ...

  8. Static块详解

    首先,我们先看一段程序,代码如下: public class Father { public Father() //构造方法 { System.out.println(" 父类构造方法&qu ...

  9. 深入浅出Zookeeper

    能找到的一些zookeeper的资料一上来不是扯一通paxos算法就是一大坨一大坨的代码.很多人对zookeeper更多的是听过,所以这一篇文章就尝试用尽可能用精简的语言科普zookeeper. zo ...

  10. 第二百七十二、三天 how can I 坚持

    昨天加班回来都很晚了,也忘了些日志了.其实感觉加班也没什么啊,一个团队在一块说说闹闹,愉快的完成工作挺好. 今天是2015年的最后一天,2015的愿望啊,只怪自己太怂了.不怂会是什么结果. 其实更应该 ...