zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    xshell下载官网地址
    logo
    网站案例搜集
    cnblogs修改网站图标icon
    父页面调用子页面方法, 子页面加载父页面传送的数据
    对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景
    关于jsp发起请求加载datagrid数据(草稿)
    接口测试-Http状态码-postman上传文件
    httpclient获取响应实体和信息的封装方法(解耦更新)
    使用httpClient调用接口获取响应数据
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3380676.html
Copyright © 2011-2022 走看看