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. Python基础:数据类型-列表与元组(6)

    Python中数据结构主要有序列.集合和字典. 1. 通用序列操作 Python内置了多种序列,其中最常用的两种:列表和元组.另一种重要的序列是字符串. 列表和元组的主要不同在于:列表是可以修改的,而 ...

  2. 聊聊 Scala 的伴生对象及其意义

    2019-04-22 关键字:Scala 伴生对象的作用 关于 Scala 伴生对象,比教材更详细的解释. 什么是伴生对象? 教材中关于伴生对象的解释是:实现类似 Java 中那种既有实例成员又有静态 ...

  3. TsinsenA1489 抽奖 【期望】

    题目分析: 问题可以转化成将m个球放进n个盒子里,每个盒子的贡献为盒子中球数的平方. 第一问考虑增量. 对于一个原本有$x$个球的盒子,新加一个球的贡献是$2x+1$.期望条件下仍然满足. 第$i$个 ...

  4. 简易RPC

    暴露服务: package com.saiarea; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; impo ...

  5. vue+axios实现文件下载

    功能:点击导出按钮,提交请求,下载excel文件: 第一步:跟后端童鞋确认交付的接口的response header设置了 axios({ method: 'post', url: 'api/user ...

  6. JS学习笔记Day20

    一. 1.服务器和客户端 客户端 程序: 通过浏览器直接运行 服务器 程序: 通过安装某种服务器软件   程序才可以运行              apache   php文件             ...

  7. golang-flag的问题

    如果选择-flag x 就是不支持布尔型

  8. 03-oracle中的高级查询

    1.连接查询 1.1 使用连接谓词指定的连接 介绍: 在连接谓词表示形式中,连接条件由比较运算符在WHERE子句中给出,将这种表示形式称为连接谓词表示形式,连接谓词又称为连接条件. 语法: [< ...

  9. Tomcat系列(2)——Tomcat文件目录7个

    核心部分 bin (运行脚本) conf (配置文件) lib (核心库文件) logs (日志目录) temp (临时目录) webapps (自动装载的应用程序的目录) work (JVM临时文件 ...

  10. Java线程池源码解析

    线程池 假如没有线程池,当存在较多的并发任务的时候,每执行一次任务,系统就要创建一个线程,任务完成后进行销毁,一旦并发任务过多,频繁的创建和销毁线程将会大大降低系统的效率.线程池能够对线程进行统一的分 ...