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 }
  • 相关阅读:
    oracle中的case when then else end 用法
    oracle中子查询
    oracle中关联查询、分组查询
    oracle中数据字典、数据、序列、索引、视图知识
    Oracle中rownum用法
    自己(转)JAVA中toString方法的作用
    自己(转)String、StringBuffer与StringBuilder之间区别
    JSP九大内置对象和四个作用域
    servlet及xml文件处理流程
    (转载)get和 post方法的不同
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6754252.html
Copyright © 2011-2022 走看看