Monthly Expense (最大值最小化+二分法)
来源:互联网 发布:cf老是提示网络异常 编辑:程序博客网 时间:2024/06/16 23:04
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 nextN (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 theith day
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
继续二分。最后right即为答案。
本题中的下界为n个数中的最大值,因为这时候,是要划分为n个区间(即一个数一个区间),left是满足题意的n个区间和的最大值,
上届为所有区间的和,因为这时候,是要划分为1个区间(所有的数都在一个区间里面), 1<=m<=n, 所以我们所要求的值肯定在 [left, right] 之间。
对于每一个mid,遍历一遍n个数,看能划分为几个区间,如果划分的区间小于(或等于)给定的m,说明上界取大了, 那么 另 right=mid,否则另 left=mid+1.
#include<cstdio> #include<cstring> #include<string>#include<iostream>#include<algorithm>using namespace std; int a[100005]; int main() { int n,m,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)/2; 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); } }
- Monthly Expense (最大值最小化+二分法)
- POJ3273 Monthly Expense(正确的二分法求最小化最大值)
- (最小化最大值)poj3273,Monthly Expense
- poj Monthly Expense(最大值最小化)
- POJ3273-Monthly Expense (最小化最大值)
- Monthly Expense(二分法)
- Monthly Expense(二分法)
- POJ 3273-Monthly Expense(二分法-最小化最高花费)
- POJ - 3273 Monthly Expense(二分搜索:最小化最大值)
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
- poj 3273 Monthly Expense (二分最小化最大值)
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
- POJ 3273 Monthly Expense【二分】(最大值最小化)
- POJ 3273 Monthly Expense(二分 最小化最大值)
- POJ 3273 Monthly Expense(二分,最大值最小化)
- POJ 3273 Monthly Expense 最大值最小 二分法
- POJ 3273 Monthly Expense (二分,最小化最大值)
- poj 3273- Monthly Expense(最大值最小化)-二分
- Spring Data JPA 吧
- java代码使用ftp上传图片失败的一个问题
- js生成固定格式的时间
- close on exec标志位
- Node.js基本模块crypto(加解密)
- Monthly Expense (最大值最小化+二分法)
- 博士第一年的计划
- 【BZOJ 1217】[HNOI2003]消防局的设立(贪心)
- python创建目录(文件夹)
- 注解结合AOP在方法前后打印日志
- jQuery实现div左右滑动
- mysql5.7解压版安装及重置密码(亲试,自整理)
- Sting&&StringBuffer&StringBuilder三者区别
- 全国省市联动