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
  • 相关阅读:
    Django集成celery发送异步邮件
    Bootstrap表格实现列左右调整大小
    Django通用类视图实现忘记密码重置密码功能
    Django项目使用ckeditor(不使用admin)
    HTML提交form表单后获取返回的数据
    Django后端发送小程序微信模板消息(服务通知)
    Django对接支付宝实现支付宝充值金币功能
    Django全文搜索django-haystack+whoosh+jieba实现中文全文搜索
    Django生成PDF文档显示在网页上以及解决PDF中文显示乱码的问题
    PHP socket 接收 java端口 netty 网络字节序
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7545796.html
Copyright © 2011-2022 走看看