POJ:3273-Monthly Expense
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32067 Accepted: 12081
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
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.
解题心得:
1. 题意就是给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是maxSum = max(sum[i]),问这个最大值最小是多少?
2. 最大值最小的问题,二分啊,每次二分枚举然后检查逐步缩小范围。
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n,m,num[maxn];
void init() {
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
}
bool checke(ll sum) {
ll temp = 0,cnt = 1;
for(int i=0;i<n;i++) {
if(num[i] > sum)
return false;
if(temp + num[i] > sum) {
temp = num[i];
cnt++;
} else
temp += num[i];
}
if(cnt <= m)
return true;
return false;
}
ll binary_search() {
ll l = 0,r = 1e9+100;
while(r-l > 1) {
ll mid = (l + r) / 2;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
}
int main() {
while(scanf("%d%d",&n,&m) != EOF ){
init();
ll ans = binary_search();
printf("%lld\n",ans);
}
return 0;
}
POJ:3273-Monthly Expense的更多相关文章
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- POJ 3273 Monthly Expense
传送门 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John is an astounding accounting wiza ...
随机推荐
- css属性(常用属性整理)
摘要:本文是我在学习前端的过程中整理的一些常用css属性,部分是css3新增的,因能力有限,文中如有错误,欢迎提出,我会及时修改.希望对大家有帮助! CSS属性 CSS属性 1 1. css颜色属性 ...
- 进一步了解this和super
知乎上看到一问题很好,拿了与大家分享,原地址:https://www.zhihu.com/question/31548104. 问: JAVA 中this 和super与覆写冲突的问题? 实例一: 输 ...
- manjaro安装后你需要做的配置
1.切换中国源 sudo gedit /etc/pacman-mirrors.conf 如果提示没有gedit , 则执行命令 : sudo pacman -S gedit 修改如下地方为中国: On ...
- 显示、更改ubuntu linux主机名(计算机名)
在bash中输入hostname可以显示计算机名.Linux和windows都可以使用这条指令. 主机名保存在/etc/hostname文件中 需要进入Root权限才可以修改该文件. sudo ged ...
- 【JavaScript 封装库】BETA 3.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- UVA-147 Dollars---完全背包+打表
题目链接: https://vjudge.net/problem/UVA-147 题目大意: 给定11种面值分别为$100, $50, $20, $10, and $5 notes and $2, $ ...
- 前端高质量知识(二)-JS执行上下文(执行环境)详细图解Script
先随便放张图 我们在JS学习初期或者面试的时候常常会遇到考核变量提升的思考题.比如先来一个简单一点的. console.log(a); // 这里会打印出什么? var a = 20; PS: 变量提 ...
- PHP编译安装时常见错误及解决办法,大全
1. configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution ...
- wordpress问题集锦
1.内容不自动换行 找到对应的样式,添加如下代码,width根据具体情况修改. width:640px;white-space:normal;word-break:break-all;word-wra ...
- 关于package.json学习
1.如果要下载npm包,必须有package.json文件,不然会报错,如果缺少必要字符报错,参考报错信息 2.license,指定用户权限,可以不写,不会报错 3.devDependencies,依 ...