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. java 基本数据类型初始值(默认值)

    1.int类型定义的数组,初始化默认是0 2.String类型定义的数组,默认值是null 3.char类型定义的数组,默认值是0对应的字符 4.double类型定义的数组,默认值是0.0 5.flo ...

  2. 洛谷P1169[ZJOI2007]棋盘制作

    题目 一道悬线法的裸题,悬线法主要是可以处理最大子矩阵的问题. 而这道题就是比较经典的可以用悬线法来处理的题. 而悬线法其实就是把矩阵中对应的每个位置上的元素分别向左向上向右,寻找到不能到达的地方,然 ...

  3. The King’s Problem HDU - 3861(连通图 缩点 匹配)

    在一个王国里面, 国王有一个新的问题. 皇城中有N个城市M条单行路,为了让他的王国更加高效,国王想要将他的王国划分成几个州,每个城市必须属于一个州.对于两个城市(u,v),必须满足以下3个条件:   ...

  4. Nginx-反向代理实现

    Nginx 反向代理操作案例 Nginx反向代理的组件模块 upstream模块介绍->点我< http_proxy_module模块介绍->点我< 环境准备 1)四台服务器都 ...

  5. Curl请求慢

    背景原因:测试环境发现一个连接内网访问和外网访问延迟差别很大,内网访问很快.外网访问很慢.于是我们用curl来诊断问题所在的区域! 命令如下: 1 curl -o /dev/null -s -w %{ ...

  6. 超越村后端开发(4:API开发)

    1.users相关的api开发 1.在settings中添加APPID,SECRET 2.在apps/users/views.py内: from chaoyuecun.settings import ...

  7. hadoop记录-hadoop集群日常运维命令

    hadoop集群日常运维命令 #1.namenode hadoop namenode -format #格式化,慎用 su hdfs hadoop-daemon.sh start namenode h ...

  8. SEO基础内容

     学习SEO人群 网络设计师 网站站长 搜索引擎相关人员 网络营销顾问 企业主 搜索引擎 Yahoo msn google baidu SEM包括SEO,竞价排名等 搜索引擎爬虫流程 派出爬虫-> ...

  9. 如何解决Java警告信息:"objc[31336]: Class JavaLaunchHelper is implemented in both places ..."

    在macOS High Sierra Version 10.13.6下使用Intellij在Java 8上执行Java程序,console中会打印如下警告信息: objc[31336]: Class ...

  10. react native头部标题样式修改

    navigationOptions: ({navigation}) => ({ headerTitle:'评估记录', headerBackTitle:null, headerLeft:null ...