[Ybtoj High-efficiency Advanced 1.2] [Greedy] Cows drying clothes

[Ybtoj High-efficiency Advanced 1.2] [Greedy] Cows drying clothes

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

Greedy first choice: Divide
the time for drying clothes,
and then judge whether you can finish drying all within this time.
You can change this time to the right border,
otherwise it will become the left border by +1.
It is said that you can use the big root pile.


Code

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,m,t,l,r,ans;
int a[500010],x[500010];  //a是直接晒干的时间,x是湿的程度
bool pd(int mid)
{
    
    
	 int j=0;  //使用甩干机的时间
	 for (int i=1;i<=n;i++)
	 {
    
    
	 	 if (a[i]>mid)
	 	 {
    
    
	 	 	int c=x[i]-mid*m;  
	 	 	if (c%t!=0)
	 	 	   j+=c/t+1;
	 	 	   else j+=c/t;  
		 } 
		 if (j>mid) return false;
	 }
	 return true;
}
int main()
{
    
    
	scanf("%d%d%d",&n,&m,&t);
	for (int i=1;i<=n;i++)
	{
    
    
		scanf("%d",&a[i]);  
		x[i]=a[i]; 
		if (a[i]%m)
		   a[i]=a[i]/m+1;
		   else a[i]=a[i]/m;
		r=max(r,a[i]);
	}
	l=1;
	while (l<r)
	{
    
    
		  int mid=(l+r)/2;
		  if (pd(mid))
		     r=mid;
		     else l=mid+1;  
	}
	printf("%d",l);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/111704862