二分法求最大值最小

POJ的3273题求最大值最小: http://poj.org/problem?id=3273

Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 44990 Accepted: 16458
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天,并已知每天的花费,要求把这N天分成连续的m组,在求得各组花费之和后,取出其中的最大值,问如何使这个最大值(令为X)尽量小。

#include<iostream>
using namespace std;
#define maxn 100005
int n,m;
int a[maxn];
int main(){
	int left=0;
    int right=0;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d",a+i);
		right+=a[i];
		if(left<a[i]) left=a[i];
	}
	while(left<right){//如果这里取等于号会无限循环
		int mind=(left+right)/2;
		int cnt=1;//分块个数初始化为1
		int sum=0;
		for(int i=0;i<n;i++){//给一个区间分成M快,使最大值最小,所以要保证每一区间和都尽可能接近mind
			if(sum+a[i]>mind){
				cnt++;//cnt++代表后面还会有一个分块所以分块的个数要加一
				sum=a[i];
			}else{
				//cnt++;
				sum+=a[i];
			}
		}
		if(cnt<=m) right=mind;//cnt小于m时说明mind过大,cnt=m时说明mind可以满足将数列分为M快的要求,但不一定是最优解,所以要将答案向左移动,求最大值最小
		else left=mind+1;//说mind小了,所以left=mind+1,来增加mind的值
	}
	printf("%d\n",left);//最后left和right都是答案
	return 0;
}

POJ2456题,求最小值最大,链接:http://poj.org/problem?id=2456
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33846 Accepted: 15491
Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don’t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input

  • Line 1: Two space-separated integers: N and C

  • Lines 2…N+1: Line i+1 contains an integer stall location, xi
    Output

  • Line 1: One integer: the largest minimum distance
    Sample Input

5 3
1
2
8
4
9
Sample Output

3
Hint

OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值