优先队列(带结构体排列方式)

优先列队与sort排序不同,sort()遇到只执行一次,再加入元素时不一定是有序的了,而优先列队在加入元素过程中时刻发生变化

头文件  #include <queue>

基本语句:priority_queue<int>rq;

基本操作:

rq.size();//返回q里元素个数
rq.empty();//返回q是否为空,空则返回1,否则返回0
rq.push(k);//在q的末尾插入k
rq.pop();//删掉q的第一个元素
rq.top();//返回q的第一个元素

非结构体型:

​常用语句:

priority_queue< int,vector<int>,less<int> > rq;//大的优先

priority_queue<int,vector<int>,greater<int>>rq;//小的优先

​

结构体型 

不做结构体内部定义 

#include<cstdio>
#include<queue>
using namespace std;
struct node
{
	int x,y;
}k;


struct cmp1    //另辟struct,排序自定义
{
	bool operator () (const node & a,const node & b) const
	{
	    if(b.x!=a.x) return b.x<a.x;
        else  return b.y>a.y;
	}
};
priority_queue <node,vector<node>,cmp1> q;  //优先列队写法
int main()
{
	k.x=10,k.y=100; q.push(k);
	k.x=12,k.y=60; q.push(k);
	k.x=14,k.y=40; q.push(k);
	k.x=6,k.y=80; q.push(k);
	k.x=8,k.y=20; q.push(k);
	while(!q.empty())
	{
		node m=q.top(); q.pop();
		printf("(%d,%d) ",m.x,m.y);
	}
}

例题1.

                                               ZOJ     11043: Mining

                                                            时间限制: 1 Sec  内存限制: 128 MB

题目描述

A mining base needs to build some robots to collect at least 10000 units of resource. Each robot will start from the base, reach the diggings in S minutes, work for W minutes, and then take C units of resource back to the base in S minutes.
  To speed up this procedure, K robots will be built at the base. It takes M minutes to produce one robot. A robot will be set to start working immediately after it is built, and producing the next robot will be on line right after. This procedure continues untill all the robots are built.
  Due to the limitation of the mining equipments, there can be only one robot digging at the working area. That is, it is only after the currently working robot finishes its collecting work and starts getting back to the base that the next robot can work at the diggings.
  Now it is your job to write a program to simulate this procedure, and find out how many minutes it will take to collect at least 10000 units of resource. 

 

输入

There are several lines of input. Each line contains a test case which consists of 5 integers, namely S, W, C, K, and M. 

输出

For each test case, you are asked to output an integer t, which is the number of minutes taken to collect at least 10000 units of resource. 

 

样例输入

复制样例数据
10 20 10 1 5

样例输出

40005

AC代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define MAX 1000000

priority_queue<int, vector<int>, less<int> > rq1;   //大的优先
priority_queue<int, vector<int>, greater<int> > rq; //小的优先

int xy[4][4]= {{-1,0},{0,-1},{1,0},{0,1}};

int w[10005];
int main()
{
    int S,W,C,K,M;
    while(cin>>S>>W>>C>>K>>M)
    {
       while (!rq.empty())
       {
           rq.pop();
       }
       memset(w,0,sizeof(w));
       if(K>10000) K=10000;
       int cnt=9999/C+1;
       for(int i=1;i<=K;i++)
       {
           w[i]=i*M+S;
           rq.push(w[i]);
       }
       int time=0;
       time=rq.top()+W;
       rq.pop();
       rq.push(time+2*S);

       for(int i=2;i<=cnt;i++)
       {
          int d=rq.top();
          rq.pop();
          if(d<=time)
            time+=W;
          else
          {
             time=d+W;
          }
            rq.push(time+2*S);
       }
       cout <<time+S<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41199502/article/details/88054992