zoukankan      html  css  js  c++  java
  • POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273

     
    Monthly Expense
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 29231   Accepted: 11104

    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

    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.二分答案,即每月的限制。
     2.枚举每一天,然后根据每月的限制,把每一天都分到一个特定的月中。如果所需的月份数小于等于M,则缩小范围,否则扩大范围。
     
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 1e5+10;
    19 
    20 int n, m;
    21 int a[MAXN];
    22 
    23 bool test(int mid)
    24 {
    25     int cnt = 0, sum;
    26     for(int i = 1; i<=n; i++)
    27     {
    28         if(a[i]>mid) return false;  //单独作为一个月都超出限定, 直接退出
    29 
    30         if(i==1 || sum+a[i]>mid)    // 重新开一个月
    31             cnt++, sum = a[i];
    32         else                        
    33             sum += a[i];
    34     }
    35     return cnt<=m;
    36 }
    37 
    38 int main()
    39 {
    40     while(scanf("%d%d",&n,&m)!=EOF)
    41     {
    42         for(int i = 1; i<=n; i++)
    43             scanf("%d", &a[i]);
    44 
    45         int l = 0, r = INF;
    46         while(l<=r)
    47         {
    48             int mid = (l+r)>>1;
    49             if(test(mid))
    50                 r = mid - 1;
    51             else
    52                 l = mid + 1;
    53         }
    54         cout<<l<<endl;
    55     }
    56 }
    View Code
  • 相关阅读:
    OpenCV开发笔记(五十八):红胖子8分钟带你深入了解图像的矩(图文并茂+浅显易懂+程序源码)
    Oracle数据迁移后由列的直方图统计信息引起的执行计划异常
    oracle统计信息的锁定与解锁
    [统计信息系列7] Oracle 11g的自动统计信息收集
    [统计信息系列6] 数据字典统计信息
    [统计信息系列5] 系统统计信息
    MongoDB创建与删除集合(collection)
    MongoDB创建与删除数据库
    [统计信息系列4] 列的统计信息
    [统计信息系列3] 索引的统计信息
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7545796.html
Copyright © 2011-2022 走看看