【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 ...
随机推荐
- 洛谷 P1881 绳子对折
P1881 绳子对折 题目描述 FJ 有一个长度为L(1<= L <= 10,000)的绳子. 这个绳子上有N(1 <= N <= 100)个结,包括两个端点. FJ想将绳子对 ...
- 【试水CAS-4.0.3】第07节_CASclient配置单点登录
完整版见https://jadyer.github.io/2015/07/26/sso-cas-client-login/ 本文源代码下载:http://download.csdn.net/detai ...
- 停止使用域名 boypay.net
停止使用域名 boypay.net boypay.net 这个域名当时注册的时候打算开发网店--" 情侣商城",表面意思是 "男生支付",情侣和婚姻是人生中必须 ...
- Servlet简单注解方式使用
我们是用Servlet进行跳转的时候都需要去web.xml中进行配置,分繁琐. 那么今天就学习下使用注解方式配置servlet一样好使 package com.shxt.servlet; import ...
- Java调用jama实现矩阵运算
Java调用jama实现矩阵运算 一.jama简介 Jama是一个基本的线性代数java包.包括一个基本的Matrix类和5个矩阵分解类. Matrix类提供了基本的线性代数数值运算的功能,不同的构造 ...
- 用实力让情怀落地!阅兵前线指挥车同款电视TCL H8800受捧
近期.一则重磅消息刷爆了平面媒体.微博.朋友圈等各个传播渠道:TCL曲面电视H8800正式入驻大阅兵前线指挥车以及国旗护卫队荣誉室.宣告代表眼下中国彩电业最高技术水准的曲面电视,正式走上大阅兵 ...
- 7.3 GROUP BY的“新”功能
7.3 GROUP BY的"新"功能正在更新内容,请稍后
- Stack overflow 编译能通过,运行时出现Stack overflow
Stack overflow 编译能通过,运行时出现Stack overflow 大家都知道,Windows程序的内存机制大概是这样的,全局变量(局部的静态变量本质也属于此范围)存储于堆内存,该段内存 ...
- 【Codeforces Round #446 (Div. 2) B】Wrath
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...
- (入门整理学习一)Asp.net core
1.安装.net code SDK,vs Code;vs code c#插件可在软件扩展 (我网盘有) vs2015上安装教程:http://www.cnblogs.com/wangrudong00 ...