B. More Cowbell
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Sample test(s)
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}.

贪心+二分(不用二分也可以)

还有,每个容器只能放两个,要符合条件,当然得最大的加最小的

当然,输入的数字已经是非递减的了,更多解释可以看代码注释

#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN = 100005;
int n, k;
LL a[MAXN];
int i,j;
bool BS(LL key)
{
int sum=0;
int l=1;
int r=n;
while(l<=r)
{
if(l!=r&&a[l]+a[r]<=key)
{
l++;
r--;
}
else
{
r--;//哎呀,大了,把右界限减少一点
}
sum++;
if(sum>k) return 0;//超过限制
}
return 1;
}
int main()
{
cin>>n>>k;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
LL l = a[n], r = 1e12, ans;
while (l <= r)
{
LL mid = (l + r) >> 1;
if (BS(mid))
{
ans = mid;
r = mid - 1;//符合条件,减小右边界,我们要的是最优的
}
else l = mid + 1;//不符合,左边界推,增大数值
}
cout<<ans<<endl;
return 0;
}

 好啦,窝萌不用二分怎么办呢,这样想,反正容器最多放二个,那么我就放K个进去,不过是从后往前放的,比如是 1 2 3 4 5   K=3  那么我先把 3 4 5放进去,把它写成公式就是还剩下n-k个,接下来还是最大的和最小的相加(相对),如 2+3  1+4  最后一个是最大的数,窝萌把它和之前的和比较 ,得出 5 。(1 4)(2 3)(5)这样符合条件

#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
int main()
{
int n,k;
int a[100010];
int i,j;
int ans1,ans2;
int sum1=0,sum2=0;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
sum1=a[n-1];
ans1=n-k-1;ans2=n-k;
for(i=ans1;i>=0;i--)
{
sum1=max(sum1,a[ans1--]+a[ans2++]);
}
cout<<sum1<<endl;
return 0;
}

  

Codeforces Round #334(div.2)(新增不用二分代码) B的更多相关文章

  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. Codeforces Round #334 (Div. 2) B. More Cowbell 二分

    B. More Cowbell Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/probl ...

  3. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

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

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

  5. Codeforces Round #523 (Div. 2)D(二分,多重集)

    #include<bits/stdc++.h>using namespace std;const long long N=1e5+5;const long long MOD=1e9+7;l ...

  6. Codeforces Round #377 (Div. 2) D. Exams(二分答案)

    D. Exams Problem Description: Vasiliy has an exam period which will continue for n days. He has to p ...

  7. Codeforces Round #334 (Div. 2)

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

  8. Codeforces Round #377 (Div. 2) D. Exams 二分

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

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

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

随机推荐

  1. android opencv

    最近工作需求:用opencv来先做一个demo.扫描照片进行边缘检测和透视矫正. 之后会加入照片降噪等处理. 请教了一下搞图像的同事.他的提议: 1.绿盟的“黄色照片检测” 用的是动态的opencv库 ...

  2. MyBatis总结六:resultMap详解(包含多表查询)

    简介: MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对 ...

  3. __get(),__set(),__isset(),__unset()

    __get(),__set(),__isset(),__unset() 在给不可访问属性赋值时,__set()会被调用读取不可访问属性的值时,__get()会被调用 当对不可访问属性调用isset() ...

  4. C++的引用的使用

    1引用的定义 引用时C++对C的一个重要的扩充,引用的作用是给变量起一个别名. 例如: int a; int &b=a;//声明b是a的引用 经过以上的声明,b就成为了a的别名,a和b的地位以 ...

  5. 【转】nginx的模块变量(HTTP核心模块变量)

    nginx的HTTP核心模块引入了大量的变量,可以在指定范围内使用这些变量的值,可以分为三类:一是客户请求头中发送的变量.二是服务器端响应头中的变量,第三是nginx产生的各种变量,我们可以使用$变量 ...

  6. 杭电acm刷题(3):1062,Text Reverse 标签: 杭电acm 2017-05-15 08:26 126人阅读 评论(0)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  7. can通信实验

    源码讲解 1.硬件连接 需要两个开发板 2.初始化函数讲解 针对F103的 3.发送函数讲解 4.接收函数讲解 5.main函数讲解

  8. 基于PhpStorm对Yii框架进行的单元测试一【PhpUnit环境搭建】

    1.下载phpunit.phar 2.在phpstorm中配置phpunit库 3.不同版本phpunit 需要依赖的php解释器也不一样,如果运行时报错 可以适当调整php解释器的版本 至此进行ph ...

  9. win10开机时不显示锁屏壁纸

    win10开机壁纸存放在此目录下: C:\Users\%username%\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManage ...

  10. Adb无法连接Genymotion

    后来注意到Genymotion启动的时候adb无法正常使用,反复调用adb启动新的server也无法解决 解决起来很简单.打开Genymotion设置界面,进入ADB标签,选中Use custom A ...