zoukankan      html  css  js  c++  java
  • POJ 3273

    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12122   Accepted: 4932

    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 ≤ MN) 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

     
    二分枚举
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 #define maxn 100005
     9 
    10 int n,m;
    11 int money[maxn];
    12 
    13 bool judge(int x) {
    14         int need = n - m,now;
    15         for(int i = 1; i <= n; i++) {
    16                 if(now + money[i] <= x) {
    17                         if(now != 0) --need;
    18                         now += money[i];
    19                 } else {
    20                         now = money[i];
    21                 }
    22 
    23         }
    24 
    25         return need <= 0;
    26 }
    27 
    28 void solve() {
    29         int l = 0,r = 0;
    30 
    31         for(int i = 1; i <= n; i++) {
    32                 l = max(l,money[i]);
    33                 r += money[i];
    34         }
    35 
    36         while(l < r) {
    37                 int mid = (l + r) >> 1;
    38                 if(judge(mid)) {
    39                         r = mid;
    40                 } else {
    41                         l = mid + 1;
    42                 }
    43         }
    44 
    45         printf("%d
    ",l);
    46 }
    47 int main() {
    48       //  freopen("sw.in","r",stdin);
    49 
    50         scanf("%d%d",&n,&m);
    51         for(int i = 1; i <= n; i++) {
    52                 scanf("%d",&money[i]);
    53         }
    54 
    55         solve();
    56 
    57         return 0;
    58 }
    View Code
  • 相关阅读:
    深圳中学校长推荐上北大 7尖子现场决出3名额
    到处Excel的数据格式设置
    转:乔布斯留给我们的十条经验
    GridView中DataFormatString属性的取值
    开发Windows服务的示例
    关于页面验证问题
    window.showModalDialog()弹出窗口获取返回值
    sql server 2000 MMC不能打开的处理方法
    基于微软平台IIS/ASP.NET开发的大型网站有哪些?
    开源相关社区/项目一览(备查,欢迎补充)
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3599189.html
Copyright © 2011-2022 走看看