Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ MN) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

Input

Line 1: Two space-separated integers: N and M
Lines 2..
N+1: Line
i+1 contains the number of dollars Farmer John spends on the
ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
题意:
就是给以一组连续的数,要求让你把他分为M组,然后让我们去求分完组后所有组的最小值,如果一组有好几个量,那就要把他们加到一起。但是在选择上面必须要选择连续的,不能跳着选择。。。我都是这一点没看到才很wa
这个样子看起来只能用二分法了,二分法要注意选择的区间,怎么用二分法,怎么选择区间,
在下面代码中,是我写的代码必须要这样选择区间,他的底限low不能小,具体看代码
代码如下:
 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f3;
int v[],n,m,w[];
int searchs(int low,int high)
{
int maxn=;
while(low<=high)
{
// printf("**%d**%d\n",low,high);
int mid=(low+high)>>;
// int temp=1;
// w[1]=v[1];
// for(int i=2;i<=n;++i)
// {
// w[i]=v[i];
// if(w[i]+w[i-1]<=mid)
// {
// w[i]+=w[i-1];
// }
// else
// {
// ++temp;
// }
// }
int temp = ,sum = ;
for(int i=; i<=n; i++)
{
if(sum+v[i] <= mid)
{
sum += v[i];
}
else
{
temp++;
sum = v[i];
}
}
if(temp==m)
{
// printf("%d*%d\n",mid,temp);
maxn=mid;
high=mid-;
}
else if(temp>m)
{
// printf("%d**%d\n",mid,temp);
low=mid+;
}
else high=mid-,maxn=mid;//printf("%d***%d\n",mid,temp);;
}
return maxn;
}
int main()
{ scanf("%d%d",&n,&m);
int a=,b=;
for(int i=; i<=n; ++i)
{
scanf("%d",&v[i]);
a+=v[i];
b=max(b,v[i]); //这里可不能在b中存输入数据的最小值,因为题目是要让
} //我们求分组后的最大值,你这样可能求出来的值小于输入的最大值
int mid=,temp=; //因为当mid<v[i]的时候temp仅仅加了一,但是这并不符合题意
int pow=searchs(b,a);
printf("%d\n",pow);
}
 
 
 
 

D - WE POJ - 3273 (二分法)的更多相关文章

  1. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  2. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  3. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  4. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  5. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  6. 补充一下我对 POJ 3273 的理解,这肯定是我一生写的最多的题解。。。

    题目:http://poj.org/problem?id=3273 当分成的组数越多,所有组的最大值就会越小或不变,这一点不难证明:    如果当前分成了group组,最大值是max,那么max的这一 ...

  7. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  8. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

  9. POJ 3273 Monthly Expense(二分搜索)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

随机推荐

  1. XML fragments parsed from previous mappers already contains value for xxxxx

    错误信息: Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file ...

  2. swiftmailer时没有设置https的选项,才可以发送成功。在linux下面

    <?php $su = 'register'; $ge = '1362836763@qq.com'; $co = 'Please register!'; send_mail($su,$ge,$c ...

  3. 关于概率dp的HINT

    摘自shadowice1984的blog 这里想讲一个关于概率题的小技巧,就是关于如何求某个事件发生的概率PP,事实上大家也清楚,除了一些特殊的近似算法之外,我们在程序中计算概率的方法无非就是加减乘除 ...

  4. x86汇编语言实践(2)

    0 写在前面 为了更深入的了解程序的实现原理,近期我学习了IBM-PC相关原理,并手工编写了一些x86汇编程序. 在2017年的计算机组成原理中,曾对MIPS体系结构及其汇编语言有过一定的了解,考虑到 ...

  5. LeetCode--689_Maximum_Sum_of_3_NonOverlapping_Subarrays

    原题链接:点击这里 一道很水很水的背包问题? 大概算不上背包吧QAQ 自己的dp 真的是太差劲啦,以后每天一道LeetCode 备战秋招! package leetcode; public class ...

  6. visp库中解决lapack库的问题

    解决的办法是——绕过去,不要用这个库: 使用中发现如下代码抛出异常: //vpTemplateTracker.cpp try { initHessienDesired(I); ptTemplateSu ...

  7. springMVC统一异常处理

    Spring MVC处理异常有3种方式: 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver: 实现Spring的异常处理接口HandlerExc ...

  8. 重学JavaScript - 数组

    作者:狐狸家的鱼 GitHub:surRimn 整理自MDN文档 数组 数组是一种类列表对象,长度和元素类型不固定. 描述 访问数组 JavaScript数组的索引是从0开始的,第一个元素的索引为0, ...

  9. ubuntu16.04安装中文输入法

    https://blog.csdn.net/u011795345/article/details/53041707

  10. oracle利用job创建一个定时任务,定时调用存储过程

    --创建表 create table TESTWP ( ID ), C_DATE DATE ); select * from TESTWP; --2.创建一个sequence create seque ...