Fish pond fishing priority queue multi-way merge sort greedy

Fish pond fishing priority queue multi-way merge sort greedy

Introduction to Priority Queue

Greedy thought: Will not look back.
Problem solving thought: enumerate 0-N-1 destination fish pond k,
Insert picture description here
as shown in the figure, select the number of t (fishing time) from top to bottom, and maximize the sum

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

priority_queue<pair<int,int> >  q;
int maxx;
int N,T;

int sum[105]; //sum[k]  表示到k鱼塘路程消耗的时间 

struct YuT{
    
       //读入鱼塘信息 
	int beg;  
	int dt;
}yy[105];



int main()
{
    
    
	cin>>N;
	int a;
	for(int i=0;i<N;i++)
	{
    
    
		scanf("%d",&yy[i].beg);
	 } 
	 for(int i=0;i<N;i++)
	 {
    
    
	 	scanf("%d",&yy[i].dt);
	 }
	 for(int i=1;i<N;i++)
	 {
    
    
	 	scanf("%d",&a);
	 	sum[i]=sum[i-1]+a;
	 }
	 cin>>T;
	 int t;              //钓鱼的时间 
	 int ans;
	 for(int k=0;k<N;k++)  //枚举目的地 
	 {
    
    
//	 	cout<<"k:"<<k<<endl;
	 	
	 	ans=0;
	 	while(!q.empty())
	 	{
    
    
	 		q.pop();
		}
	 	
	 	t=T-sum[k];
	 	
	 	if(t<=0)              
	 	 continue;
	 	
//	 	cout<<"t:"<<t<<endl;
	 	
	 	for(int i=0;i<=k;i++)             //把一路的头入队列 
	 	{
    
    
	 	    q.push({
    
    yy[i].beg,i});           //需要记录鱼塘代号,以便入下个队列 
		}
		
		int near;
		
		while(!q.empty())                   //某一路可掉的鱼数归0了 
		{
    
    
			pair<int,int> s=q.top();          //每次钓1min鱼 
			q.pop();
			t--;                   
			ans+=s.first;                    //掉一分钟  钓鱼总数增加 
			
//			cout<<s.first<<" "<<s.second<<endl; 
			
			if(!t)                           //时间用完了,跳出循环 
 			{
    
    
				break;
			}
			
			near=s.first-yy[s.second].dt;      //计算 下一个入队列的 
			if(near>0)
				q.push({
    
    near,s.second});
			
		}
		maxx=max(maxx,ans);                 
	 	
	 }
	 cout<<maxx;
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45448563/article/details/113774982