zoukankan      html  css  js  c++  java
  • Monthly Expense POJ

      

    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 ≤ MN) 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

    Hint

    If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<string>
     5 #include<cstring>
     6 #include<cmath>
     7 #define MAX 100005
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 int n,m;
    12 int a[MAX];
    13 
    14 bool check(double ans)
    15 {   double temp=0,t=0;
    16     for(int i=1;i<=n;i++){
    17         temp+=a[i];
    18         if(temp>ans){
    19             t++;
    20             temp=a[i];
    21         }
    22     }
    23     t++;
    24     if(t<=m) return true;
    25     else return false;
    26 }
    27 int main()
    28 {   while(~scanf("%d%d",&n,&m)){
    29        int ma=0,sum=0;
    30        for(int i=1;i<=n;i++){
    31              scanf("%d",&a[i]);
    32              ma=max(ma,a[i]);
    33              sum+=a[i];
    34        }
    35        double l=ma,r=sum;
    36        double mid;
    37        while(r-l>1e-9){
    38              mid=(l+r)/2.0;
    39              if(check(mid)) r=mid;
    40              else l=mid;
    41        }
    42        printf("%d
    ",int(l+0.5));
    43     }
    44 }
  • 相关阅读:
    SQL Server 数据库部分常用语句小结(三)
    SQL Server 数据库部分常用语句小结(四)
    通过存储过程(SP)实现SQL Server链接服务器(LinkServer)的添加
    pcb布线强弱电间隔距离
    程序占用内存大小
    Offer来了(原理篇)笔记之第三章并发编程
    Offer来了(原理篇)笔记之第一章JVM原理
    西瓜视频奇妙的bug
    mongodb忘记了admin的账号密码
    MongoDB更改默认端口
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6754252.html
Copyright © 2011-2022 走看看