【31.72%】【codeforces 604B】More Cowbell
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
input
2 1
2 5
output
7
input
4 3
2 3 5 9
output
9
input
3 2
3 5 7
output
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}.
【题目链接】:http://codeforces.com/contest/604/problem/B
【题解】
二分最后的箱子容量;
左端点应该是最大的cowbell,右端点无限大.
看看m需要用几个箱子d;
(如果加上这个数大于m或装了两个就不装了);
(装的时候,先装大的,大的尝试和当前剩余最小的组合在一起,如果能组合就组合,不能的话大的单独装.);
if (d <= k)
ans = m,r = m-1;
else
if (d > k)//箱子用多了那就增大箱子容量
l = m+1;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n,k;
int s[MAXN];
int ok(int lim)
{
int tot = 0;
int r = n,l = 1;
while (l <= r)
{
if (l!=r && s[r]+s[l]<=lim)
{
r--,l++;
tot++;
}
else
tot++,r--;
}
return tot;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
int l = 1,r = 21e8;
rep1(i,1,n)
{
rei(s[i]);
l = max(s[i],l);
}
int ans = -1;
while (l <= r)
{
int m = (l+r)>>1;
if (ok(m)<=k)
{
ans = m;
r = m-1;
}
else
l = m+1;
}
cout << ans << endl;
return 0;
}
【31.72%】【codeforces 604B】More Cowbell的更多相关文章
- 【CodeForces 604B】F - 一般水的题1-More Cowbe
Description Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 760A】Petr and a calendar
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【21.37%】【codeforces 579D】"Or" Game
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 关于oracle sql语句查询时 表名和字段名要加双引號的问题具体解释
作为oracle的刚開始学习的人相信大家一定会遇到这个问题.如图: 明明就是navicat可视化创建了表,但是就是不能查到!这个为什么呢? select * ...
- Android app : use html or native?
Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...
- battery-获取手机电量信息
我们如果想要获得手机的电池电量信息,可以借助广播来实现.因为当手机电池电量发生变化的时候,系统会发送一个广播.具体代码如下 //注册 intentFilter.addAction(Intent.ACT ...
- 37.Node.js工具模块---处理和转换文件路径的工具 Path模块
转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方 ...
- Mybatis批量插入,是否能够返回id列表
第1次代码 void batchAdd(List<Photo> list); <insert id="batchAdd" parameterType=" ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-1/2
原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 本文主要讲述了如何使用Multiplayer Networking开发多人游 ...
- 洛谷 P2384 最短路
洛谷 P2384 最短路 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你10000000000000000000000000000 ...
- Java中的线程模型及实现方式
概念: 线程是一个程序内部的顺序控制流 线程和进程的比较: 每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大. 线程:轻量的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和 ...
- 洛谷 P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- [Vue + TS] Using Route events inside Vue
vue-router introduces new hooks into the component. In this lesson we’ll show you how to use these n ...