#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
ifstream fin("C:\\data6.in");
int arr[32767];
int cnt;
int m;
int sum;
int cn=0;
void Init()
{
int x;
cnt=0;
sum=0;
cin>>m;
while(fin>>x)
{
arr[cnt++]=x;
sum+=x;
}
}
int GetMaxNum()
{
int max=arr[0];
for(int i=1;i<cnt;++i)
{
if(max<arr[i])
max=arr[i];
}
return max;
}
int GetAnswer(int beg,int end)
{
int count=0;
int tempsum=0;
int ans=1;
for(int i=0;i<cnt;++i)
{
if(tempsum+arr[i]<=beg)
{
tempsum+=arr[i];
}
else
{
++count;
if(count>=m)
{
ans=0;
break;
}
tempsum=arr[i];
}
}
return ans;
}
int findMinMax()
{
int beg,end;
beg=GetMaxNum();
end=sum;
while(beg!=end&&end-beg!=1)
{
int ans=GetAnswer(beg,end);
if(ans==0/*划分的数量大于m,beg偏小*/)
{
beg+=(end-beg)/2;
}
else/*beg可能偏大*/
{
beg-=(end-beg)/2;
end-=2*(end-beg)/3;
}
}
cout<<"最小最大值为:"<<beg<<endl;
}
int main()
{
Init();
findMinMax();
system("pause");
return 0;
}
最小最大值
最新推荐文章于 2022-07-24 21:43:41 发布