AOJ-AHU-OJ-466 Monthly Expense

37 篇文章 0 订阅
20 篇文章 0 订阅
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.

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
OriginalTransformed
7 5
100
400
300
100
500
101
400

Sample Output
OriginalTransformed
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
————————————————————无脑的分割线————————————————————
思路:考虑两个方面: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值