AOJ-AHU-OJ-466 Monthly Expense
来源:互联网 发布:剑网3御姐捏脸数据 编辑:程序博客网 时间:2024/06/16 10:18
Monthly Expense
Time Limit: 2000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB
Judge By Case
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.
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
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
OriginalTransformed7 5100400300100500101400
Sample Output
OriginalTransformed500
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
————————————————————无脑的分割线————————————————————
思路:考虑两个方面:1.解的取值范围 2.怎样判定解为最优解
首先,在最乐观的情况下,答案是输入数据当中的最大值。那么不那么乐观呢?假如用最大值无法完成所需要的分配数怎么办呢?毫无疑问需要扩大。扩大多少呢?最大不会超过所有数据的和。
那么问题变成了从Max到Sum之间找到最优解。二分法应该是最快的。
其次,判定最优解。我们写一个Check函数,用找到的解去分配,如果分配任务完成,或者分配量富余,则继续分配。直到找到了唯一个解,使得不存在比它小又能完成分配的解。
代码如下:
————————————————————无脑的分割线————————————————————
思路:考虑两个方面:1.解的取值范围 2.怎样判定解为最优解
首先,在最乐观的情况下,答案是输入数据当中的最大值。那么不那么乐观呢?假如用最大值无法完成所需要的分配数怎么办呢?毫无疑问需要扩大。扩大多少呢?最大不会超过所有数据的和。
那么问题变成了从Max到Sum之间找到最优解。二分法应该是最快的。
其次,判定最优解。我们写一个Check函数,用找到的解去分配,如果分配任务完成,或者分配量富余,则继续分配。直到找到了唯一个解,使得不存在比它小又能完成分配的解。
代码如下:
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>int a[100010], day, month;int check(int midi){ int Mon = 1, sum = 0; for(int i = 0; i < day; i++){ if(sum + a[i] <= midi) sum += a[i]; else{ sum = 0; Mon++; i--; } } if(Mon <= month) return 1;//该解足以完成分配要求,返回1,继续寻找更优解 else return 0;//不能完成,说明需要更大的预算}int binS(int l, int r){//二分模板,注意区间的端点。以下写法是左开右闭区间 int mid; while(l < r){ mid = (l+r) >> 1; if(check(mid)) r = mid;//寻找更优解 else l = mid + 1;//更大的预算 } return r;}int main(){int maxi = -1, sum = 0;while(~scanf("%d%d", &day, &month)){ int n = day; for(int i = 0; i < n; i++){ scanf("%d", a+i); sum += a[i];//计算总和 if(a[i] > maxi) maxi = a[i];//寻找最大值 } int ans = binS(maxi, sum+1); printf("%d\n", ans); }return 0;}
0 0
- AOJ-AHU-OJ-466 Monthly Expense
- AOJ-AHU-OJ-2 Homework
- AOJ-AHU-OJ-9 University
- AOJ-AHU-OJ-11 Rails
- AOJ-AHU-OJ-542 Window
- AOJ-AHU-OJ-489 夺宝奇兵
- AOJ-AHU-OJ-676 投票
- AOJ-AHU-OJ-531 IPv4
- AOJ-AHU-OJ-31 Counting Sheep
- AOJ-AHU-OJ-6 Hero in Maze
- AOJ-AHU-OJ-3 Redraiment的遭遇
- AOJ-AHU-OJ-5 渊子赛马
- AOJ-AHU-OJ-7 Redraiment猜想
- AOJ-AHU-OJ-8 童年生活二三事
- AOJ-AHU-OJ-394 Fibonacci numbers
- AOJ-AHU-OJ-401 Fibonacci & GCD
- AOJ-AHU-OJ-12 Box of Bricks
- AOJ-AHU-OJ-592 神奇的叶子
- APP Manifest -Android
- Android webview doesn't display web page in some cases
- HTTP POST GET 本质区别详解
- http://blog.sina.com.cn/s/blog_8fbcc2dc01013yn2.html
- 木马攻击与防范
- AOJ-AHU-OJ-466 Monthly Expense
- 【类】项目三。多文件
- 概率论 基本概念
- 非root权限解决/usr/lib64/libstdc++.so.6: version过低问题
- linux 添加系统调用方法(修正)
- 加密解密算法,客户端C++加密,服务端PHP解密
- 【Android学习】Android完全退出应用程序
- Spring JdbcTemplate方法详解 .
- 自然月