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

  • 相关阅读:
    [Linux]Vmwaer创建CENTOS7虚拟机[转]
    [游记]二访金陵
    [Android]ADB调试: SecurityException: Injecting to another application requires INJECT_EVENTS permission
    [操作系统]记一次未尽的三星 Galaxy A6s(SM-G6200)刷机过程
    [网络]NAT与内网穿透技术初探【待续】
    结构化系统建模之程序流程图|系统流程图|数据流图
    UML系统建模之用例视图
    [Linux]常用命令之【mkdir/touch/cp/rm/ls/mv】
    [Linux]监控外部用户登录及外部主机连接情况
    [Java SE]Unicode解码
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3380676.html
Copyright © 2011-2022 走看看