POJ3273-Monthly Expense
来源:互联网 发布:软启动器编程 编辑:程序博客网 时间:2024/05/21 13:54
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
题意:共n个月,给出每个月的开销,将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值
解题思路:二分
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int a[100009];int n,m;bool check(int k){ int sum=0,cnt=0; for(int i=1;i<=n;i++) { if(a[i]>k) return 0; if(sum+a[i]<k) sum+=a[i]; else if(sum+a[i]==k) {sum=0,cnt++;} else {sum=a[i];cnt++;} } if(sum) cnt++; if(cnt<=m) return 1; else return 0;}int main(){while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); int l=0,r=INF,ans; while(l<=r) { int mid=(l+r)>>1; if(check(mid)) {r=mid-1,ans=mid;} else l=mid+1; } printf("%d\n",ans); }return 0;}
- POJ3273:Monthly Expense
- POJ3273:Monthly Expense(二分)
- POJ3273--Monthly Expense
- POJ3273:Monthly Expense(二分)
- POJ3273 Monthly Expense
- poj3273 Monthly Expense
- poj3273 Monthly Expense
- 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
- POJ3273-Monthly Expense
- POJ3273 Monthly Expense(二分法)
- 十二、解析H.264码流的宏块结构(上)
- Windows下获取硬件信息的编程(CPU、硬盘、内存和启动时间等)
- wildfly服务器系列--wildfly运行standalone启动时报错
- Effective C++ 第四条 确定对象被使用前以先被初始化
- Implementation of typical layout--section 1
- POJ3273-Monthly Expense
- C++ 正则表达式
- 关于对UEditor富文本框所获取的数据做“筛选”处理
- bond网卡绑定配置
- Apache CXF拦截器Interceptor实现WebServices用户验证
- MyEclipse将Java项目打包成jar文件的三种方法
- 第5章 散列
- 十三、熵编码算法(3):CAVLC原理
- ubnt 路由器 switch0 和 br0的区别