POJ 3273 Monthly Expense (二分搜索)

来源:互联网 发布:流量联网控制软件 编辑:程序博客网 时间:2024/05/18 08:42

Monthly Expense
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16796 Accepted: 6660

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 5100400300100500101400

Sample Output

500

跟上一题类似 也是选出m个连续day 使得fajmonth小于等于二分的答案

AC代码如下:

////  POJ 3273 Monthly Expense////  Created by TaoSama on 2015-04-24//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;int n, m, a[N];bool check(int x) {    int last = 0;    for(int i = 1; i <= m; ++i) {        int idx = last + 1, sum = a[idx];        while(idx <= n && sum < x) {            ++idx;            sum += a[idx];        }        if(idx == n + 1) return false;        last = idx - 1;    }    return true;}int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);//  freopen("out.txt","w",stdout);#endif    ios_base::sync_with_stdio(0);    cin >> n >> m;    for(int i = 1; i <= n; ++i) {        cin >> a[i];    }    int l = 0, r = 1e9 + 1;    while(l + 1 < r) {        int mid = 0LL + l + r >> 1;        if(check(mid)) l = mid;        else r = mid;    }    cout << l << '\n';    return 0;}


0 0
原创粉丝点击