Monthly Expense poj3273 (二分/最大值最小化)
来源:互联网 发布:南京黑马程序员培训班 编辑:程序博客网 时间:2024/05/21 09:58
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
题目大意:这种题目看懂了题目就很好敲了
给出n,m
一共有n个连续的金额
问分成m组使得每组分的最少
求分组中最大的那组的数值
所以答案最大是sum 就是所有的分一个组
最小是maxn 即n=m的时候每个自己一个组
So 利用二分在这区间寻找合适的答案
#include <iostream>#include <cstring>#include <cstdio>using namespace std;int num[100100];int main(){int n,m,i;int sum;int maxn;while(cin>>n>>m){sum = 0;maxn = 0;for(i=1;i<=n;i++){cin>>num[i];sum+=num[i];maxn=max(maxn,num[i]);}while(maxn < sum) ///花费分组后最小就是maxn 最大 sum{int cnt=0; ///分了几组int temp_sum=0;int mid = (maxn+sum)>>1;for(i=1;i<=n;i++){temp_sum+=num[i];if(temp_sum > mid) ///如果 总和 大于mid 那么这个num[i]的分到下一个分组中{temp_sum=num[i];cnt++;}}if(cnt < m) ///如果分的cnt组小于m组 那么上界小点{sum = mid;}else{maxn = mid+1;}}cout<<maxn<<endl;}return 0;}
- Monthly Expense poj3273 (二分/最大值最小化)
- POJ3273-Monthly Expense (最小化最大值)
- (最小化最大值)poj3273,Monthly Expense
- POJ3273:Monthly Expense(二分)
- POJ3273:Monthly Expense(二分)
- poj3273--Monthly Expense(二分)
- poj3273 Monthly Expense(二分)
- POJ3273 Monthly Expense 二分
- 二分 Monthly Expense poj3273
- POJ3273 Monthly Expense(正确的二分法求最小化最大值)
- POJ 3273 Monthly Expense (二分,最小化最大值)
- poj 3273- Monthly Expense(最大值最小化)-二分
- POJ3273 Monthly Expense 二分搜索
- poj3273 Monthly Expense(二分)
- POJ3273:Monthly Expense(二分)
- POJ3273 Monthly Expense 二分查找
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
- POJ - 3273 Monthly Expense(二分搜索:最小化最大值)
- 好用的maven中央仓库,阿里云提供
- Hibernate中的主要概念
- git学习
- POJ-1611 The Suspects (并查集)
- android自定义之Dialog
- Monthly Expense poj3273 (二分/最大值最小化)
- 树莓派上的软件安装和卸载命令
- linux的启动过程与系统调用过程
- Unity引擎(物理引擎上)
- jQuery tab切换收集
- LeetCode-Hash-242. Valid Anagram
- 第一次写博客
- Chapter 5: 关联式容器之 set 和 multiset
- 欢迎使用CSDN-markdown编辑器