• 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
  • 相关阅读:
    产品小细节中的大体验
    产品经理的四点思考:不该简单满足用户需求
    产品经理的十大顶级错误
    SQL Server数据库大型应用解决方案总结
    java中public static void main(String[] args)中String[] args代表什么意思?
    异常处理1
    java中的String
    华为2013年西安java机试题目:如何过滤掉数组中的非法字符。
    2用java代码实现冒泡排序算法(转载)
    1用java实现冒泡排序算法以及解决的几个小问题。
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7545796.html
Copyright © 2020-2023  润新知