POJ3273 二分
2016-02-20 14:49
337 查看
Monthly Expense
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
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
Sample Output
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.
题目大意:
一共有n天,分成m份,如何让每个值都尽可能的大,并且求出这些值的最大值。
思路:
刚开始是想着用for循环,循环到n来一个一个计算的,但是发现这种方法并不好。因为这样子结束条件并不好找,在取条件的时候需要特别留意。于是看了一下书,书上有一道例题是POJ的2456的,和牛之间距离最远有关系。它的方法是把牛放到for循环中,然后n是用来条件判断的。感觉这个样子边界条件找寻比较方便。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20335 | Accepted: 8005 |
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.
题目大意:
一共有n天,分成m份,如何让每个值都尽可能的大,并且求出这些值的最大值。
思路:
刚开始是想着用for循环,循环到n来一个一个计算的,但是发现这种方法并不好。因为这样子结束条件并不好找,在取条件的时候需要特别留意。于是看了一下书,书上有一道例题是POJ的2456的,和牛之间距离最远有关系。它的方法是把牛放到for循环中,然后n是用来条件判断的。感觉这个样子边界条件找寻比较方便。
//相当于有N个牛棚,M + 1头牛,让着几头尽可能远 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 100000 + 10; const int inf = 0x3f3f3f3f; int n, m; int d[maxn]; bool C(int mid){ int last = 0, cnt = 0; for (int i = 1; i <= m; i++){ cnt = last; int sum = d[last]; while (cnt < n && sum < mid){ sum += d[++cnt]; } if (cnt == n){ // printf("i = %d\n", i); return false; } last = cnt; } return true; } void solve(){ int lb = 0, ub = inf;//限定最小值的高度 while (ub - lb > 1){ int mid = lb + (ub - lb) / 2; if (C(mid)) lb = mid; else ub = mid; } printf("%d\n", lb); } int main(){ while(scanf("%d%d", &n, &m) != EOF){ memset(d, 0, sizeof(d)); for (int i = 0; i < n; i++){ scanf("%d", d + i); } solve(); } return 0; } </cmath></algorithm></cstring></cstdio>
相关文章推荐
- 初学者github会遇到的错误
- Android Animations动画使用详解
- 知识点链接
- Hibernate 3中如何获得库表所有字段的名称
- VisualRust + VisualGDB编辑调试Rust
- 猜数字游戏
- 十六进制的字符串与二进制字符串相互转换
- jquery 对象dom 对象互转
- example of Makefile to compile a c source file
- eclipse下的spring环境配置
- 经典SQL语句大全
- 【杭电oj】1875 - 畅通工程再续(克鲁斯塔尔)
- 收入成本预算_直营
- bsoj 2044 【SCOI2007】最大土地面积
- 【Lintcode】Median of two Sorted Arrays
- 选择本地图片上传到服务器进行头像设置以及文件下载
- 通用刷新数据源代码
- The 5th Zhejiang Provincial Collegiate Programming Contest------ProblemK:Kinds of Fuwas
- 如何在 Go 语言中使用 Redis 连接池-Radix.v2
- 【C语言】文件常用读写操作(含读取学生信息示例)