poj 3273 Monthly Expense 【二分搜索(最大化最小值)】
来源:互联网 发布:阿里云提供哪些服务 编辑:程序博客网 时间:2024/05/16 23:42
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 18798
Accepted: 7515
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 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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on theith day
Output
Sample Input
7 5100400300100500101400
Sample Output
500
Hint
Source
//s的大小最容易搞迷了,所以你要一边写代码,一遍写注释,来提示自己 #include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int N,M;int a[1000005];int C(int s){int cnt=1;int sum=0;for(int i=0;i<N;i++){if(sum+a[i]<=s){sum+=a[i];}else{sum=a[i];cnt++;}}if(cnt<=M)//说明分的组数太少了,也就是s设的太大了! return 1;else//s设的太小了! return 0;}int main(){while(scanf("%d%d",&N,&M)!=EOF){int max=0,min=0;for(int i=0;i<N;i++){scanf("%d",&a[i]);if(min<a[i])min=a[i];//a中的最大值作为下线 max+=a[i];//所有元素的和作为上线 }int l=min-1,r=max+1;while(r-l>1)//不能写成while(r>l),会形成死循环 {int mid=(l+r)/2;if(C(mid))//条件成立,说明s太大了,所以你可以将s调小一点 r=mid;//也就是将另上线为中间值 else//否则说明s太小了,你需要找一个大一点的值 l=mid;//让下线为中间值! }printf("%d\n",r);}return 0;}
//错误代码!不知道为什么错了! #include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int N,M;int a[1000005];int C(int s){int cnt=1;int sum=0;for(int i=0;i<N;i++){sum=a[i];int j;for(j=i+1;j<N;j++){if(sum+a[i]<=s){sum+=a[i];}else{break;}}i=j-1;cnt++;}if(cnt<=M)//说明分的组数太少了,也就是s设的太大了! return 1;else//s设的太小了! return 0;}int main(){while(scanf("%d%d",&N,&M)!=EOF){int max=0,min=0;for(int i=0;i<N;i++){scanf("%d",&a[i]);if(min<a[i])min=a[i];//a中的最大值作为下线 max+=a[i];//所有元素的和作为上线 }int l=min-1,r=max+1;while(r-l>1)//不能写成while(r>l),会形成死循环 {int mid=(l+r)/2;if(C(mid))//条件成立,说明s太大了,所以你可以将s调小一点 r=mid;//也就是将另上线为中间值 else//否则说明s太小了,你需要找一个大一点的值 l=mid;//让下线为中间值! }printf("%d\n",r);}return 0;}
- 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 二分搜索
- POJ 3273 - Monthly Expense(二分搜索)
- poj 3273 Monthly Expense(二分搜索)
- POJ - 3273 Monthly Expense(二分搜索:最小化最大值)
- 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 (二分)
- POJ 3273-Monthly Expense(二分+贪心)
- ThinkPHP框架的使用
- 交流总结
- <LeetCode><Easy> 83 Remove Duplicates from Sorted List
- java中String与Int的相互转换
- 三、在何处放置 VBScript
- poj 3273 Monthly Expense 【二分搜索(最大化最小值)】
- cocos2dx 3.6版本 初学者遇到的问题
- 软件测试
- The Clean Architecture
- OpenCV findContours 轮廓检索函数及轮廓特征计算
- jdk1.6中垃圾收集器
- jquery图片传送带
- 滴滴快的_2016_面试题_笔试题_产品经理_题来了
- 浮点数储存理解误区