POJ-3273 Monthly Expense 解题报告
2012-03-16 01:33
465 查看
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
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.
题目链接:http://poj.org/problem?id=3273
解法类型:二分
解题思路:money和M之间为单调递减函数关系,即对money进行二分。但要注意边界的处理。
算法实现:
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.
题目链接:http://poj.org/problem?id=3273
解法类型:二分
解题思路:money和M之间为单调递减函数关系,即对money进行二分。但要注意边界的处理。
算法实现:
//STATUS:C++_AC_110MS_512K #include<stdio.h> const int MAXN=100010; int judge(int mid); int mon[MAXN],N,M; int main() { // freopen("in.txt","r",stdin); int i,low,mid,high,tot; while(~scanf("%d%d",&N,&M)) { low=0x80000000; for(i=0,high=1;i<N;i++){ scanf("%d",&mon[i]); high+=mon[i]; if(mon[i]>low)low=mon[i]; } while(high-low!=1){ mid=(low+high)/2; tot=judge(mid); if(tot>M)low=mid; else high=mid; } mid=judge(low)<=M?low:high; //这里要注意 printf("%d\n",mid); } return 0; } int judge(int mid) //计算M { int i,tot,s,count; for(i=0,tot=0;i<N;count==1?i++:0){ for(s=0,count=0;i<N;i++){ s+=mon[i]; count++; if(s>mid)break; } tot++; } return tot; }
相关文章推荐
- POJ-3273 Monthly Expense 解题报告
- POJ 3273 Monthly Expense 解题报告(二分枚举)
- poj解题报告——3273
- POJ 2743 字符串判等 解题报告
- POJ-1654 Area 解题报告
- POJ 3518 Prime Gap 解题报告
- POJ 1635 解题报告
- POJ1659 Frogs' Neighborhood ACM解题报告(图论基础 邻接矩阵)
- POJ 2478 Farey Sequence 解题报告(欧拉函数 筛求法)
- poj 3370 Halloween treats 解题报告 <鸽巢原理>
- POJ 1915解题报告
- 【原创】POJ 1703 && RQNOJ 能量项链解题报告
- poj解题报告——1035
- [二分+模拟]poj 3273 Monthly Expense
- poj――校门外的树解题报告
- poj解题报告_1753
- poj-3273 Monthly Expense (二分)
- POJ 2480 Longge's problem 解题报告(欧拉函数 + 积性函数)
- POJ-1284-Primitive Roots 解题报告
- poj 1041-Dividing解题报告-可行性背包问题