CSU-ACM2017暑假集训2-二分搜索 poj-32732 Monthly Expense
来源:互联网 发布:手机看pdf软件 编辑:程序博客网 时间:2024/06/05 02:39
题目:
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.
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
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
7 5100400300100500101400
500
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.
题意:有一连串的日子,农场主对每天要花的支出都有记录,现在他想把这些单独的N天压缩为M个月,每个月包含连续的天数(数量任意)
,并给这些月份分配一个总支出限额(也就是这个月里的每天的支出之和不能超过的数),要求这个限额最小是多少。
思路:二分答案,注意对边界的计算。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m;int money[111111];int maxm;int judge(int x){ int counts,l,r; counts=l=0; r=1; while(1) { int sum=money[l]; if(sum>x) return 0; while(r<n) { if(sum+money[r]<=x) sum+=money[r++]; else break; } l=r++; counts++; if(l==n) { if(counts<=m) return 1; else return 0; } }}int bs(int l,int r){ int mid; while(l<=r) { mid=(r-l)/2+l; if(judge(mid)) r=mid-1; else l=mid+1; } return r+1;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { maxm=-1; for(int i=0;i<n;i++) { scanf("%d",&money[i]); maxm=max(maxm,money[i]); } int ans=bs(0,maxm*m); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- CSU-ACM2017暑假集训2-二分搜索 poj-32732 Monthly Expense
- Monthly Expense--CSU-ACM2017暑假集训2-二分搜索
- csu-acm2017暑假集训2-二分搜索D
- CSU-ACM2017暑假集训2-二分搜索 hdu2119
- CSU-ACM2017暑假集训2-二分搜索 F
- CSU-ACM2017暑假集训2-二分搜索 E
- CSU-ACM2017暑假集训2-二分搜索 A
- CSU-ACM2017暑假集训2-二分搜索 poj3104-drying
- CSU-ACM2017暑假集训2-二分搜索 D
- CSU-ACM2017暑假集训2-二分搜索 C
- CSU-ACM2017暑假集训2-二分搜索 poj-2785-4 Values whose Sum is 0
- CSU-ACM2017暑假集训2-二分搜索 poj-2456 Aggressive cows-最大化最小值
- CSU-ACM2017暑假集训2-二分搜索 poj-3258- River Hopscotch
- CSU-ACM2017暑假集训2-二分搜索 hdu2141- Can you find it?
- Can you find it? --CSU-ACM2017暑假集训2-二分搜索
- Can you solve this equation?--CSU-ACM2017暑假集训2-二分搜索
- 4 Values whose Sum is 0 --CSU-ACM2017暑假集训2-二分搜索
- CSU-ACM2017暑假集训比赛2 C
- 矩阵乘法
- 对象
- 从leetcode 92.reverse-linked-list-ii 对链表的认识
- React Native加载图片详解
- mysql5.7.18修改或者设置密码
- CSU-ACM2017暑假集训2-二分搜索 poj-32732 Monthly Expense
- 【主流身份管理技术辨析】Authentication and Authorization: OpenID vs OAuth2 vs SAML
- Data Binding学习(二)
- 136. Single Number
- tarjan二分图匹配问题
- ios-关于cell的模板
- python 的 Pillow实现图片对比
- 简易手动部署多节点的Openstack(L版)——陆(安装Dashboard服务)
- JS/HTML格式化显示