[Ybt] [Basic calculation greedy class example 1] Cows drying clothes

Cows drying clothes

Topic link: Cow drying clothes


Title description

Insert picture description here
Insert picture description here

Problem solving ideas

Obviously this is a greed.
We use the dryer for the wettest clothes every time.
But this is O (n 2) O(n^2)O ( n2 )The algorithm will time out.
We use the heap to optimize, the time complexity isO (n log ⁡ n) O(n\log{n})O ( nlogn)

code

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

priority_queue<int>q;

int n,a,b,t;

int main()
{
    
    
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
    
    
		int t;
		scanf("%d",&t);
		q.push(t);
	}
	while(q.top()>t*a)
	{
    
    
		int s=q.top();
		q.pop();
		q.push(s-b);
		t++;
	}
	cout<<t<<endl;
}

Guess you like

Origin blog.csdn.net/SSL_guyixin/article/details/111706575
Recommended