构造 牛客小白月赛85E 烙饼

链接

挺好一道题

我们设理想时间为 所有饼的总时长之和/锅数 向上取整

饼最大值和理想时间取最大值为答案,设答案为t

方案是顺序烙饼,每t时间切割一次放入新的锅里

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,t,maxx,cnt;
const int N=100010;
int a[N],ans[N<<1][5];
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;++i)scanf("%lld",&a[i]),t+=a[i],maxx=max(maxx,a[i]);t=(t+m-1)/m;
	if(maxx>t)t=maxx;
	for(int i=1,now1=1,now2=0;i<=n;++i)
		if(now2+a[i]<=t)
		{
			++cnt;
			ans[cnt][1]=i;ans[cnt][2]=now1;ans[cnt][3]=now2;ans[cnt][4]=now2+a[i];
			now2+=a[i];
			if(now2==t)++now1,now2=0;
		}
		else
		{
			++cnt;
			ans[cnt][1]=i;ans[cnt][2]=now1;ans[cnt][3]=now2;ans[cnt][4]=t;
			++cnt;
			ans[cnt][1]=i;ans[cnt][2]=now1+1;ans[cnt][3]=0;ans[cnt][4]=a[i]-(t-now2);
			++now1,now2=a[i]-(t-now2);
		}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;++i,printf("\n"))
		for(int j=1;j<=4;++j)printf("%lld ",ans[i][j]);
	return 0;
}
/*
3 2
2 2 2
*/
posted @ 2024-01-09 18:16  wljss  阅读(4)  评论(0编辑  收藏  举报