• 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

  • 相关阅读:
    ps背景橡皮擦工具详解
    Application called By IE on Pseudo B/S Mode
    一种B/S模式下基于JAVA反射机制的可伸缩动态加载模块的解决方案
    Compile the latest Kernel(linux3.1rc4) On Ubuntu Plateform
    HttpService & WebService For Flex Develop
    国外的一个flex入门教学~[转]
    HQL略解
    How to build a Flex development platform based on Myeclipse8.6 & Flex4
    基于Pipe的PureMVC Flex框架的多核共享消息技术
    Ninject IOC<一>
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3380676.html
Copyright © 2020-2023  润新知