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
Output
Sample Input
Sample Output
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.
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[100005];
int main()
{
int n,m,mid;
while(scanf("%d%d",&n,&m)==2)
{
int sum=0;
int maxn=-1;
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>maxn) maxn=a[i];
sum+=a[i];
}
mid=(maxn+sum)/2;
while(maxn<sum) //注意这里是<!
{
int cnt=1;
int s=0;
for(i=1;i<=n;i++)
{
s+=a[i];
if(s>mid)
{
cnt++;
s=a[i];
}
}
if(cnt<=m) sum=mid-1;
else maxn=mid+1;
mid=(maxn+sum)/2;
}
printf("%d\n",mid);
}
return 0;
}
max型:
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up toM rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Output
Sample Input
Sample Output
Hint
代码如下:
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int a[50000+5];
int main()
{
int l,n,m;
while(scanf("%d%d%d",&l,&n,&m)==3)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
a[0]=0;a[n+1]=l;
sort(a+1,a+1+n);
int minn=a[1]-a[0],maxx=l;
for(int i=1;i<=n+1;i++) minn=min(minn,a[i]-a[i-1]);
int mid=(minn+maxx)>>1;
while(minn<=maxx) //注意这里是<=!
{
int cnt=-1;
int s=0;
for(int i=1;i<=n+1;i++)
{
s+=a[i]-a[i-1];
if(s>=mid)
{
cnt++;
s=0;
}
}
if(cnt<n-m) maxx=mid-1;
else minn=mid+1;
mid=(minn+maxx)>>1;
}
printf("%d\n",mid);
}
}
至于证明过程还是将中间变量都printf出来看看好了,之前都想破头脑才搞清楚的= =。
至于实数型的二分或三分只要设一个eps即可,用<就行(因为区间变化是取mid的,所以如果相等就会一直处于那个值出不来了。)
但是有一个问题,精度的问题,如2.005用%.2f输出是2.01,若想要结果是2.00,则可以这样写:
if((int)(high*1000)%10>=5) high-=0.005;
或者乘100用floor在除以100。
下面给一个例子:
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input data,scanf is recommended.
代码如下:
}