poj 3273 Monthly Expense(二分搜索之最大化最小值)
2015-09-03 18:52
453 查看
Description
Input
Output
Sample Input
Sample Output
Hint
Source
USACO 2007 March Silver
View Code
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
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
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
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.
Source
USACO 2007 March Silver
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define ll long long #define N 100006 #define inf 1<<30 int n,m; int a ; bool solve(int x){ int sum=0; int group=0; for(int i=0;i<n;i++){ if(sum+a[i]<=x){ sum+=a[i]; } else{ sum=a[i]; group++; } } if(group>=m) return false;//如果当前的mid值能分成大于等于m的组数,说明mid太小,mid变大的时候组数才能变少 return true; } int main() { while(scanf("%d%d",&n,&m)==2){ int high=0; int low=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); high+=a[i]; if(a[i]>low){ low=a[i];//把花费最多的那天当做是最低low(相当于把n天分为n组) } } while(low<high){ int mid=(low+high)>>1; if(solve(mid)){ high=mid; } else{ low=mid+1; } } printf("%d\n",low); } return 0; }
View Code
相关文章推荐
- Python Serial 与STM32J进行串口通讯
- nb
- 面试热问——你在前一份工作(实习)学到什么?
- Activity的四种启动模式
- java简单的字符串大小比较——compareTo()方法
- exit()与_exit()的区别
- HDU 5095 Linearization of the kernel functions in SVM
- 从NFS启动Linux系统,OK6410
- 在Ubuntu搭建Git
- 第三讲:post-processsing with vcs+ files
- 类的构造函数与析构函数的调用顺序
- 如何为你的平台部署一个公开的系统状态页
- http协议
- C++实现斐波那契数列/青蛙跳台阶
- 文章分享:简单数据结构学习:单向链表
- 细说Java多线程之内存可见性
- 第一部分、十道海量数据处理面试题
- java中连接数据库操作的语句,获得执行sql语句对数据库进行操作
- fis3简单教程
- S路,定点停车起步 心得