您的位置:首页 > 其它

POJ3273 二分

2016-02-20 14:49 337 查看
Monthly Expense

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 20335Accepted: 8005
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.

题目大意:

一共有n天,分成m份,如何让每个值都尽可能的大,并且求出这些值的最大值。

思路:

刚开始是想着用for循环,循环到n来一个一个计算的,但是发现这种方法并不好。因为这样子结束条件并不好找,在取条件的时候需要特别留意。于是看了一下书,书上有一道例题是POJ的2456的,和牛之间距离最远有关系。它的方法是把牛放到for循环中,然后n是用来条件判断的。感觉这个样子边界条件找寻比较方便。

//相当于有N个牛棚,M + 1头牛,让着几头尽可能远
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
int n, m;
int d[maxn];

bool C(int mid){
int last = 0, cnt = 0;
for (int i = 1; i <= m; i++){
cnt = last;
int sum = d[last];
while (cnt < n && sum < mid){
sum += d[++cnt];
}
if (cnt == n){
//	   printf("i = %d\n", i);
return false;
}
last = cnt;
}
return true;
}

void solve(){
int lb = 0, ub = inf;//限定最小值的高度
while (ub - lb > 1){
int mid = lb + (ub - lb) / 2;
if (C(mid)) lb = mid;
else ub = mid;
}
printf("%d\n", lb);
}

int main(){
while(scanf("%d%d", &n, &m) != EOF){
memset(d, 0, sizeof(d));
for (int i = 0; i < n; i++){
scanf("%d", d + i);
}
solve();
}
return 0;
}
</cmath></algorithm></cstring></cstdio>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: