[Ybtoj Chapter 2 Example 1] Cows drying clothes [greedy] [big root pile]

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here


Problem solving ideas

Note that the answer is related to the time required to dry the last piece of clothing. Regardless of the dryer, the last piece of clothing to be dried must be the one with the highest humidity. So we hope that the humidity of the clothes with the highest humidity is as low as possible.

So get a greedy strategy: choose the clothes with the highest humidity to use the dryer every time.

Simulate the process of drying clothes for a cow, and set the current as the t-th minute, if ai − t ∗ A <= 0 a_i-t*A<=0aitA<=0 , it can be known that the i-th garment is dried. Such time complexity isO (n 2) O (n^2)O n2 ), so we can use a large root pile to maintain the humidity of the clothes, reducing the time complexity toO (nlogn) O(n log n)O n l o g n


Code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int> q;

int t,n,A,B,a[500010];

int main(){
    
    
	scanf("%d%d%d",&n,&A,&B);
	for(int i=1;i<=n;i++)
	{
    
    
		scanf("%d",&a[i]);
		q.push(a[i]);
	}
	while(q.top()-t*A>0)
	{
    
    
		t++;
		int x=q.top();
		q.pop();
		q.push(x-B);
	}	
	printf("%d\n",t);
}

Guess you like

Origin blog.csdn.net/kejin2019/article/details/111707422
Recommended