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.