zoukankan      html  css  js  c++  java
  • POJ 3273

    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12122   Accepted: 4932

    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 ≤ 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.

    Source

     
    二分枚举
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 #define maxn 100005
     9 
    10 int n,m;
    11 int money[maxn];
    12 
    13 bool judge(int x) {
    14         int need = n - m,now;
    15         for(int i = 1; i <= n; i++) {
    16                 if(now + money[i] <= x) {
    17                         if(now != 0) --need;
    18                         now += money[i];
    19                 } else {
    20                         now = money[i];
    21                 }
    22 
    23         }
    24 
    25         return need <= 0;
    26 }
    27 
    28 void solve() {
    29         int l = 0,r = 0;
    30 
    31         for(int i = 1; i <= n; i++) {
    32                 l = max(l,money[i]);
    33                 r += money[i];
    34         }
    35 
    36         while(l < r) {
    37                 int mid = (l + r) >> 1;
    38                 if(judge(mid)) {
    39                         r = mid;
    40                 } else {
    41                         l = mid + 1;
    42                 }
    43         }
    44 
    45         printf("%d
    ",l);
    46 }
    47 int main() {
    48       //  freopen("sw.in","r",stdin);
    49 
    50         scanf("%d%d",&n,&m);
    51         for(int i = 1; i <= n; i++) {
    52                 scanf("%d",&money[i]);
    53         }
    54 
    55         solve();
    56 
    57         return 0;
    58 }
    View Code
  • 相关阅读:
    redis持久化RDB与AOF
    oracle PL/SQL习题
    pl/sql中的三种循环
    转 Oracle Cursor用法总结
    Oracle与MySQL的SQL语句区别
    安装mysql 遇到最后一步卡死解决方案
    Oracle安装
    mysql安装
    sql语句的优先级
    MVC设计模式
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3599189.html
Copyright © 2011-2022 走看看