神奇
查看原帖
神奇
780763
chinese_hacker楼主2023/10/17 20:38

关于我用二分写出了一个随机数程序

#include<iostream>
using namespace std;
int n,timemax;
int a[10005];
bool check(int x)
{
	int time=0;
	int now=1;
	int others=n;
	while(true)
	{
		if(others>=x)
		{
			for(int i=1;i<=x;i++)
			{
				time+=a[now];
				now++;
			}
			others-=x;
		}
		else
		{
			for(int i=1;i<=others;i++)
			{
				time+=a[now];
				now++;
			}
			break;
		}
//		cout<<others<<' '<<now<<' '<<time<<endl;
	}
	if(time<=timemax)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int f()
{
	int l=1,r=n,ans;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(check(mid))
		{
			r=mid-1;
			ans=mid;
		}
		else
		{
			l=mid+1;
		}
	}
	return ans;
}
int main()
{
	cin>>n>>timemax;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	cout<<f();
	return 0;
}

求助!

2023/10/17 20:38
加载中...