最大值最小化
2015-05-29 20:14
295 查看
1043: 最大值最小化
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 79 Solved: 14
[Submit][Status][Web
Board]
Description
把一个包含n个正整数的序列划分成m个连续的子序列(每个正整数恰好属于一个序列)。设第i个序列的各数之和为S(i),设S(1),…,S(m)的最大值为MAX,请通过划分使得MAX最小。例如序列1 2 3 2 5 4划分为3个子序列的最优方案为 1 2 3 | 2 5 | 4,其中S(1),S(2),S(3)分别为6,7,4,那么最大值为7;
如果划分为 1 2 | 3 2 | 5 4,则最大值为9,不是最小。
Input
输入包含多组测试样例。以文件尾为结束。第一行包含两个数字n(0<n<=100000),m(0<m<=n).n表示序列的数字个数,m表示划分为m个子序列。
第二行包含n个正整数a1,…ai,…an.(0<ai<=10^9)
Output
输出MAX的最小值。每个输出占一行。
Sample Input
6 3 1 2 3 2 5 4 5 2 4 2 1 8 3
Sample Output
7 11
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
long long c[222222];
int Max, n,m;
long long sum = 0;
bool is(long long x)
{
long long s = 0;
int cent = 0;
for(int i = 0 ; i < n ; i++)
{
if(s + c[i] > x)
{
cent++;
s = c[i];
if(cent == m)
{
return false;
}
}
else s += c[i];
}
return true;
}
long long answer()
{
long long x,y;
x = Max , y = sum;
while(x < y)
{
long long mid = x + (y-x)/2;
if(is(mid)) y = mid;
else x = mid + 1;
}
return x;
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
Max = 0;
sum = 0;
for(int i = 0 ; i < n ; i++)
{
scanf("%d",&c[i]);
if(c[i] > Max) Max = c[i];
sum += c[i];
}
long long ans = answer();
printf("%lld\n",ans);
}
return 0;
}
/**************************************************************
Problem: 1043
User: AC_XXZ
Language: C++
Result: Accepted
Time:136 ms
Memory:3424 kb
****************************************************************/
相关文章推荐
- oc中的点语法讲解
- 浏览器的兼容性问题实质是CSS的兼容性问题
- Hadoop(一)之Hadoop安装的坑爹经历
- 昨日今日大盘下跌分析
- C 初始化数组
- java中多线程笔记(二)
- Androidapi.JNI.Net.Wifi.hpp文件
- SQL、LINQ和Lambda表达式
- 杭电ACM2018java做法
- Windows通过VNC或SSH远程登录Mac
- char*和char[]
- Android 开发工具类 24_getHtml
- 手机Java/Android编程IDE----AIDE的快捷键整理
- 巧用“快指针”、“慢指针”
- bellman-ford算法
- MATLAB 数据分析二: 样本数据可视化--条形图、直方图、二/三维散点图
- 欧拉工程第44题:Pentagon numbers
- HDU--2028
- pthread相关
- Java与多线程