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 }
  • 相关阅读:
    将 SharePoint 2010 网站集升级到 2013 (含沙盒方案)
    几款网络云存储服务的使用对比
    技术发展飞快,一日十年
    项目背景介绍
    初次接触,简单的了解需求
    用色彩区分 SharePoint 2010 Calendar 的日历项
    嘿,我这里有一个 Survey!
    博客页面在 IE 浏览器中样式混乱了(已经更换了样式)
    关于 Graphviz
    搭建使用 RTX51Tiny 的 C51 Keil 项目环境
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6754252.html
Copyright © 2011-2022 走看看