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. 解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题

    前言 SpringBoot使用MultiPartFile接收来自表单的file文件,然后进行服务器的上传是一个项目最基本的需求,我以前的项目都是基于SpringMVC框架搭建的,所以在使用Spring ...

  2. springboot 的部分细节

    Application.properties 中#指定端口号 server.port= #指定访问路径必须以/crud/xxx 开始 server.servlet.context-path=/crud ...

  3. 2019-04-04 Mybatis学习知识点

    1. 比较#和$的区别 #是占位符?,$是字符串拼接.因此使用$的时候,如果参数是字符串类型,那么要使用引号 尽量使用#而不是$ 当参数表示表名或列名的时候,只能使用$ 2. 多参数时候 配置文件中使 ...

  4. 关于小米4电信4g刷入第三方ROM无信号解决办法

    from: http://www.yuwantb.com/xiaomi4-lineage-os.html   关于小米4电信4g刷入第三方ROM无信号解决办法 下载这个电信4g补丁包. 链接:http ...

  5. shutil、zipfile,tarfile

    shutil 模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. 1. shutil.copyfileobj(fsrc, fdst[, length]) 功 ...

  6. P2447 [SDOI2010]外星千足虫 (高斯消元)

    题目 P2447 [SDOI2010]外星千足虫 解析 sol写到自闭,用文字描述描述了半个小时没描述出来,果然还是要好好学语文 用高斯消元求解异或方程组. 因为 \(奇数\bigoplus奇数=偶数 ...

  7. <Android基础> (七)内容提供器

    第七章 内容提供器 7.1 内容提供器(Content Provider) 主要应用于在不同的应用程序之间实现数据共享功能.允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性. 7.2 ...

  8. 01Design and Analysis Algorithm Using Python-程振波

    1.(p14)比较两个数的大小 a = int(input('num:')) b = int(input('num:')) def getMax(a,b): if a>b : print('Th ...

  9. Docke--基础篇

    什么是Docker? Docker 最初是dotCloud公司创始人Solomon Hykes在法国期间发起的一个公司内部项目,它是基于dotCloud公司多年云服务技术的一次革新,并与2013年3月 ...

  10. ajax+json

    ajax学习: 1.ajax的概念 局部刷新技术.不是一门新技术,是多种技术的组合,是浏览器端的技术 2 为什么要使用ajax?           传统的模式 是 发送请求 到 服务器 ,服务器经过 ...