POJ3273 Monthly Expense(正确的二分法求最小化最大值)
来源:互联网 发布:java随机生成二维数组 编辑:程序博客网 时间:2024/05/24 07:28
Link:http://poj.org/problem?id=3273
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5100400300100500101400
Sample Output
500
Hint
Source
AC code:
#include<iostream>#include<stdio.h>#include<algorithm>#include<math.h>#include<string.h>#include<map>#include<set>#define LL long long#define INF 0xfffffffusing namespace std;int a[100010];int n,m;int group(int mid){int sum=0,cnt=1,i;for(i=0;i<n;i++){sum+=a[i];if(sum>mid){cnt++;sum=a[i];}}return cnt;}/* int Bsearch(int low,int high)//错误的二分写法!!! {int mid=(low+high)/2;while(low<=high){if(group(mid)<=m){high=mid-1;}else{low=mid+1;}mid=(low+high)/2;}return mid;}*/ int Bsearch(int low,int high)//正确的二分写法!!! {int mid;while(low<=high){mid=(low+high)/2;if(group(mid)<=m){high=mid-1;}else{low=mid+1;}}return mid;} int main(){//freopen("in.txt","r",stdin);int i,low,high,ans;while(scanf("%d%d",&n,&m)!=EOF){high=0;low=0;for(i=0;i<n;i++){scanf("%d",&a[i]);high+=a[i];low=max(low,a[i]);}ans=Bsearch(low,high);printf("%d\n",ans);}return 0;}
- POJ3273 Monthly Expense(正确的二分法求最小化最大值)
- (最小化最大值)poj3273,Monthly Expense
- POJ3273-Monthly Expense (最小化最大值)
- Monthly Expense (最大值最小化+二分法)
- Monthly Expense poj3273 (二分/最大值最小化)
- POJ3273 Monthly Expense(二分法)
- POJ3273 Monthly Expense 二分法
- (POJ3273)Monthly Expense <二分法 + 贪心>
- poj Monthly Expense(最大值最小化)
- poj3273 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 (二分解决最小化最大值)
- Word2003入门动画教程79:在Word中插入Excel图表
- 【Scikit-Learn 中文文档】支持向量机
- httpclent调用webservice
- 机器学习-----线性回归浅谈(Linear Regression)
- java 接口套三个Json的实现方式
- POJ3273 Monthly Expense(正确的二分法求最小化最大值)
- idempiere 汉化/新建实体
- java上传下载文件部署到linux系统下的一些问题
- 分类模型与算法--线性判别分析
- java 写文件的三种方法比较
- velocity模板保留两位小数(截断2位)
- mysql插入数据量过大报错解决办法
- 2017.10.23学习知识总结回顾及编写新网页
- 用C语言编写程序在屏幕上打印杨辉三角。(非递归)