Description

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.
 
 
题意:这题看了好久都没看懂题意,就是将序列划分为k段,要求找出k段中最大的资金和要在所有符合要求的划分中值最少
思路:我们可以直到这个值必然在所有数中最大值与所有数的总和之间,那么只要再这个区间进行二分即可
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[100005];
int main()
{
int n,m;
int sum,maxn,i,j,s,cnt,mid;
while(~scanf("%d%d",&n,&m))
{
sum = 0,maxn = 0;
for(i = 0; i<n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
maxn = max(maxn,a[i]);
}
while(maxn<sum)
{
mid = (sum+maxn)>>1;
s = 0,cnt = 0;
for(i = 0; i<n; i++)
{
s+=a[i];
if(s>mid)
{
s = a[i];
cnt++;
}
}
if(cnt<m) sum = mid;
else maxn = mid+1;
}
printf("%d\n",maxn);
} return 0;
}

POJ3273:Monthly Expense(二分)的更多相关文章

  1. POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273   Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Tota ...

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

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

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

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

  4. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

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

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

  6. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  7. POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25959   Accepted: 10021 ...

  8. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  9. POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7630 ...

随机推荐

  1. urlwrite伪静态(SAE、PHP、JSP)

    在SAE里,直接配置config.yaml文件,文件可以配置的内容包含: 目录默认页面 自定义错误页面 压缩 页面重定向 页面过期 设置响应Header的Content-Type appname: x ...

  2. mac在 aliyun linux ecs实例上安装 jdk tomcat mysql

    用了一个ftp 工具 把 gz rpm 等 传递到ecs 上 -- 用这个Transmit 用ssh远程登录,然后依次安装 jdk tomcat  mysql 到 /usr/local/... 设置环 ...

  3. Reverse Interger

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  4. recvmsg和sendmsg函数

    这两个函数是最通用的I/O函数.实际上我们把所有read.readv.recv和recvfrom调用替换成recvmsg调用.类似地,各种输出函数调用也可以替换成sendmsg调用 #include ...

  5. 路由器to路由器

    本文主要介绍一个主路由器连接两个子路由器的方法: 主路由器: 设置主路由器的开始ip地址为192.168.1.100,结束ip地址为192.168.150: 子路由器A: 第一步:LAN设置 第二步: ...

  6. grails框架中在使用domain的save方法保存时保存不成功

    1.如果报错,自行根据异常查找错误,这里不说明 2.如果为报错,我遇到的就是domain中的字段属性与数据库中为同步 (1)你的domain是新的,在增加新的字段属性时未使用update更新数据库,造 ...

  7. nodejs 初学笔记

    首先到nodejs的官网安装nodejs,地址nodejs.org,网站第一页会根据你的电脑系统推荐你适合的版本,下载,不断next,在cmd中输入 node -v 可以看到版本的话,即安装成功. 说 ...

  8. 利用花生壳在自己电脑上建立外网可访问的svn

    下载花生壳并注册账号 2.花生壳会送你一个免费的二级域名 3.登陆到路由器界面192.168.0.1或者192.168.0.0进入动态dns选项输入你的花生壳账号密码 4.在路由器设置界面设置转发规则 ...

  9. 转:ElasticSearch的安装和相关插件的安装

    原文来自于:http://blog.csdn.net/whxaing2011/article/details/18237733 本文主要介绍如下内容:          1.ElasticSearch ...

  10. 转:在虚拟机中用NAT方式连接网络

    1.安装VMware Workstation .在安装过VMware Workstation软件后,会在本地连接中,多了两个虚拟网卡,一个是 VMware Network Adapter for VM ...