Problem 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 ≤ M ≤ N) 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

题意:给出n天每天的花费,将n天分成m组,且分组的天数是连续的,要求分完各组的花费和尽量小,输出各组花费和的最大值。

思路:一开始以为是 dp ,写了写没写出来突然想到要求各组最小和的最大值应该用二分做,顺利AC。。。

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100001
#define MOD 123
#define E 1e-6
using namespace std;
int n,m;
int a[N];
bool judge(int value)//判断当前花费可把n分成几组
{int sum=0;//花费总和int cnt=1;//初始时只有一组for(int i=0;i<n;i++)//枚举每天花费{if(sum+a[i]<=value)sum+=a[i];else{sum=a[i];cnt++;}}if(cnt>m)//若分组数比规定数要多,则mid偏小,需调整左值return false;else//若分组数比规定数要少,则mid偏大,需调整右值return true;
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF){int left=0,right=0;for(int i=0;i<n;i++){scanf("%d",&a[i]);left=max(left,a[i]);right+=a[i];}int mid;while(left<=right){mid=(left+right)/2;if(judge(mid))right=mid-1;elseleft=mid+1;}printf("%d\n",mid);}return 0;
}

Monthly Expense( POJ-3273 )相关推荐

  1. Monthly Expense(二分专题)

    题目连接:https://vjudge.z180.cn/contest/413975#problem/B 题目 农民约翰是一个令人震惊的会计巫师,他已经意识到他可能没有钱来经营这个农场.他已经计算并记 ...

  2. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  3. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  4. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  5. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  6. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  7. Monthly Expense(二分)

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

  8. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  9. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  10. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

最新文章

  1. 用深度学习实现异常检测/缺陷检测
  2. FastJson 原理
  3. 推荐算法-聚类-层次聚类法
  4. Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Canvas)
  5. 《架构之美》阅读笔记二
  6. SQLServer2008快速清理日志文件
  7. Linux环境搭建nginx负载
  8. 第三次学JAVA再学不好就吃翔(part77)--迭代器遍历
  9. linux启动日志服务命令,linux命令:syslog日志服务-系统缩减之增加日志服务功能...
  10. 合数分解1(C语言)
  11. angular.isString()
  12. 黑马品优购项目的总结-首页
  13. 基于Java的酒店管理系统论文
  14. 子龙山人 opengl系列 - 网址
  15. 河南城建学院的计算机科学与技术,河南城建学院计算机科学与工程系
  16. MS11-003在Internet Explorer中存在允许远程代码执行漏洞复现
  17. Ubuntu系统搭建PPPoE服务器,Ubuntu上架设PPPoE Server
  18. 社会管理网格化 源码_张家口市召开市域社会治理暨全市网格化服务管理现场观摩会议...
  19. 用 Python 分析各国人口性别比例
  20. SDS之Object Storage: 对象存储 - 生于时代,长于场景

热门文章

  1. 学习全球最火编程语言Python,要读哪些书?
  2. 教育部认定,“新工科”最有“钱途”
  3. php5.6 xdebug 配置,php5.5+apache2.4+mysql5.6+xdebug配置
  4. 15 张图, 把TCP/IP 讲得一清二楚!
  5. 醉了!吃着火锅哼着歌,男朋友强行给我科普什么是补码!
  6. Java程序员必备:常见OOM异常分析
  7. TCP/IP入门(1) --链路层
  8. Socket编程实践(7) --Socket-Class封装(改进版v2)
  9. Vue-cli(四) 项目中引入Axios
  10. Heron 数据模型,API和组件介绍