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

  • 相关阅读:
    20.11.16 leetcode406 leetcode中的排序写法
    20.11.15 leetcode402
    20.11.14 leetcode1122(自定义排序)
    polyline NOIP模拟 数论 规律
    alien NOIP模拟 位运算 数论
    跳石头 vijos1981 NOIP2015 D2T1 二分答案 模拟 贪心
    寻找道路 vijos1909 NOIP2014 D2T2 SPFA
    不死的LYM NOIP模拟 二分+状压DP
    死亡的颂唱者 NOIP模拟 贪心 DFS
    无线网络发射器选址 vijos 1908 NOIP2014 D2T1 模拟
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3380676.html
Copyright © 2011-2022 走看看