今天还是刷二分法的题,有两道题把我弄糊涂了,首先看着两道题:
Monthly Expense
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 120 Accepted Submission(s) : 38
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.
7 5 100 400 300 100 500 101 400
500
AC代码:
/*
题意:就是将序列划分为k段,要求找出k段中最大的资金和要在所有符合要求的划分中值最少
思路:我们可以直到这个值必然在所有数中最大值与所有数的总和之间,那么只要再这个区间进行二分即可
*/
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m,maxn,sum,left,right,mid;
int a[100001];
while(cin>>n>>m&&m>=1&&n>=1)
{ sum=0;maxn=0;
for(int i=1;i<=n;i++)
{cin>>a[i];
sum+=a[i];
maxn=max(maxn,a[i]);
}
right=sum;
left=maxn;
while(right>left)
{ int s=0,cnt=0;
mid=(right+left)/2;
for(int i=1;i<=n;i++)
{ s+=a[i];
if(s>mid)
{
s=a[i];
cnt++;
}
}
if(m>cnt) right=mid;
else left=mid+1; //为什么此处m<=cnt中,当m=cnt时,要更新left而不更新right,如果更新right,wrong answer。这跟下一题的这一步骤正相反
}
cout<<left<<endl;
}
}
Drying
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 93 Accepted Submission(s) : 30
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
<b>sample input #1</b> 3 2 3 9 5 <b>sample input #2</b> 3 2 3 6 5
<b>sample output #1</b> 3 <b>sample output #2</b> 2
AC代码:
/*
题目大意:
有n件衣服,每件衣服含a滴水
有一台每分钟可以烘干k滴水的机器,每次可以让一件衣服使用
衣服每分钟蒸发一滴水,输出烘干所有的衣服的最少时间
算法分析:
二分枚举最少时间mid
遍历衣服,若某件衣服含水量大于mid
算出烘干所用总时间sum;
我们二分弄干的时间 t
那么对于含水量 <= t 的那些衣服 不用管它们 让它们自然风干就好了
对于含水量 >t 的那些衣服 假如说其含水量为 a[i]
设 x为烘烤该件衣服的时间 y为风干的时间
那么
x + y = t
x * k + y >= a[i]
整理得
x >= (a[i] - t) / ( k - 1)
这样对于每件衣服 我们都可以求得用这么长的时间最少的烘烤所用的时间(这些衣服在不烘烤的时间段里,自然风干,总时间为t)
求出总和与二分结果比较
再判断时间与mid的大小
注意k=1的时候特殊处理
*/
#include<stdio.h>
#include<math.h>
int a[100010];
int main()
{
int i,n,k,max=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>max) max=a[i];
}
scanf("%d",&k);
int mid,left=1,right=max;
if(k==1) left=right;
while(left<right)
{
long long sum=0;
mid=(left+right)/2;
for(i=1;i<=n;i++)
if(a[i]>mid) sum+=ceil((a[i]-mid)*1.0/(k-1));
if(sum>mid) left=mid+1;
else right=mid;//为什么此处的sum<=mid中,当sum=mid时,要更新right而不是更新left,如果更新left,时wrong answer
}
printf("%d\n\n",left);
return 0;
}