贪心:502. IPO

题目:

在这里插入图片描述

来源:。。

分析:

很容易想,考虑用结构体优先队列。

代码:

struct node1{//按照本金大小   从小到大排列的   暂时还不能进行的项目。 
 int x,y;//x是本金    y是净赚 
 bool operator < (const node1&a) const
 {
  return x>a.x;//x小的在队列首部 
 }
}; 
struct node2{//按照净赚的大小  从大到小进行排列   都是可以进行的项目  
 int x,y;//x是本金    y是净赚 
 bool operator < (const node2&a) const
 {
  return y<a.y;//y大的在队列的头部 
 }
};
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        if(profits.empty()) return 0;
        struct node1 n1;
 struct node2 n2; 
  priority_queue<node1> q1;
 priority_queue<node2> q2;
 
 for(int i=0;i<profits.size();i++)
 {
  n2.x=n1.x=capital[i];
  n2.y=n1.y=profits[i];
  if(capital[i]<=w){
   q2.push(n2);
  }
  else q1.push(n1);
 }
 for(int i=0;i<k;i++)
 {
  if(q2.empty()) break;
  w+=q2.top().y;
  q2.pop();
  
  while((!q1.empty()) && w>=q1.top().x)
  {
   n1=q1.top();
   q1.pop();
   n2.x=n1.x;
   n2.y=n1.y;
   q2.push(n2);
  }  
 }
 return w;
     }
};

总结:

应该牢记结构体优先队列。

发布了48 篇原创文章 · 获赞 20 · 访问量 614

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/103300163
IPO