• Monthly Expense 和上题一样 二分 找到最接近的最优值


    Problem 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
    **************************************************************************************************************************
    **************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #include<algorithm>
     7 #define  inf  9999999999
     8 #define  LL  long long
     9 using namespace std;
    10 int a[100011];
    11 int low,high,mid;
    12 int n,m,i,j,k;
    13 int main()
    14 {
    15     scanf("%d%d",&n,&m);
    16     high=0;
    17     low=-inf;
    18     for(i=1;i<=n;i++)
    19     {
    20         scanf("%d",&a[i]);
    21         high+=a[i];
    22         if(low<a[i])
    23           low=a[i];
    24     }
    25     int get_m,sum;
    26     while(low<high)
    27     {
    28         mid=(low+high)/2;
    29         sum=0;
    30         get_m=0;
    31         for(i=1;i<=n;i++)
    32         {
    33             if(sum+a[i]<=mid)
    34             {
    35                 sum+=a[i];
    36             }
    37             else
    38             {
    39                 sum=a[i];
    40                 get_m++;
    41             }
    42         }
    43         if(get_m<m)high=mid;
    44         else
    45            low=mid+1;
    46     }
    47     printf("%d
    ",low);
    48 }
    View Code

  • 相关阅读:
    android_SurfaceView 画图
    android_activity_研究(二)
    android_sdcard读写(一)
    双缓冲的小程序
    C++运算符重载小程序
    再练
    菜鸟的第一个博客(用java写的个小文本框)
    LUCENE第一个小程序(有错)
    蜗牛在奔跑
    指定目录下的java文件存储到一个Txt文本文件中
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3380676.html
Copyright © 2020-2023  润新知