C++ notes: queue queue, priority queue priority_queue, double-ended queue deque

I spent three days writing it, and I am exhausted , ⏬, if it is useful to you, please like it~

table of Contents

NO.1 queue

1. What is a queue

2. Some concepts of queue

3. Queue in C++STL (hereinafter referred to as queue) operation

Four. Queue example

NO.2 priority queue

1. What is priority queue

2. Priority_queue operation in C++STL

3. Priority queue example

NO.3 double-ended queue

1. What is a double-ended queue (deque)

2. Priority_queue operation in C++STL

3. Deque example


NO.1 queue

1. What is a queue

Queue is a linear data structure like stack and dynamic array. The rule is: elements added first are deleted first, that is, " first in , first out ", which is the opposite of stack. The queue can only add elements at the end of the queue and delete elements at the head of the queue.

2. Some concepts of queue

  • Head of the queue: The end of the queue that allows delete operations.
  • End of the queue: The end of the queue that allows insertion operations.
  • Enqueue: Insert operation of the queue.
  • Dequeue: delete operation of the queue.
  • Here is the legend:

3. Queue in C++STL (hereinafter referred to as queue) operation

#include<queue> //用队列要调用的头文件
queue<int>q; //定义一个名字为q、数据类型为int的队列
q.empty(); //如果队列为空返回true,否则返回false
q.size(); //返回队列中元素的个数
q.pop(); //删除队列首元素但不返回其值
q.front(); //返回队首元素的值,但不删除该元素
q.push(); //在队尾压入新元素
q.back(); //返回队列尾元素的值,但不删除该元素

Four. Queue example

Insert picture description here

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    int m,n,k;
    cin>>n>>m>>k;
    queue<int>q1,q2;
    for(int i=1;i<=n;i++)q1.push(i);
    for(int i=1;i<=m;i++)q2.push(i);
    while(k--)
    {
        cout<<q1.front()<<" "<<q2.front()<<endl;
        q1.push(q1.front());
        q2.push(q2.front());
        q1.pop();
        q2.pop();
    }
    return 0;
}

NO.2 priority queue

1. What is priority queue

Priority queues are similar to queues, but with priority . The priority queue will sort the inserted elements from large to small by default, and the internal is done by the heap. For example, in a hospital, critical and emergency patients can certainly not queue up for treatment like ordinary patients. At this time, we need a priority queue so that we can access the high priority elements first.

2. Priority_queue operation in C++STL

#include<queue> //优先队列要调用的头文件
priority_queue<int>pq; //声明了一个int类型的将元素优先级高的排在前面的优先队列
priority_queue<int,vector<int>,greater<int> > pq_2; /*声明了一个int类型的将
元素优先级低的排在前面的优先队列(要#include<vector>),为了避免>>被认为成右移符
号,在中间最好打上空格。*/
pq.top(); //返回队头元素的值但不删除
pq.empty(); //队列是否为空,空则返回0,否则返回1
pq.size(); //返回队列内元素个数
pq.push(); //插入元素到队尾 (并排序)
pq.pop(); //弹出队头元素

Priority queues can also customize their priority . Readers can search on Baidu, so I won’t say more here (I don’t understand this knowledge...), it looks like this:

/定义比较结构
struct cmp1{
    bool operator ()(int &a,int &b){
        return a>b;//最小值优先
    }
};

struct cmp2{
    bool operator ()(int &a,int &b){
        return a<b;//最大值优先
    }
};

//自定义数据结构
struct number1{
    int x;
    bool operator < (const number1 &a) const {
        return x>a.x;//最小值优先
    }
};
struct number2{
    int x;
    bool operator < (const number2 &a) const {
        return x<a.x;//最大值优先
    }
};

3. Priority queue example

The topic link can be seen: http://codeforces.com/contest/1015/problem/C

Code:

#include<iostream>
#include<queue>
using namespace std;
const int N=1e5+7;
priority_queue<int>q;
int n,m,i,j,k;
long long sum;
int main(){
    cin>>n>>m;
	for(k=1;k<=n;++k){
        scanf("%d%d",&i,&j);
        sum+=i;
        q.push(i-j);
	}
	while(!q.empty()&&sum>m){
        sum-=q.top();
        q.pop();
	}
	if(sum<=m)cout<<n-q.size()<<endl;
	else puts("-1");
}

NO.3 double-ended queue

1. What is a double-ended queue (deque)

The deque is a variant of the queue. In general, the queue can only add elements at the end of the queue (push) and delete elements at the head of the queue (pop), while the deque performs addition and deletion at the head or the end of the queue at the same time. jobs.

2. Priority_queue operation in C++STL

#include<deque> //引入双端队列要调用
deque<int>dq; //定义一个int类型的双端队列dq
dq.push_back(); //在队列尾部添加元素,无返回值。
dq.push_front(); //在队列头部添加元素,无返回值;
dq.pop_back(); //删除队列尾部的元素,无返回值;
dq.pop_front(); //删除队列头部的元素,无返回值;
dq.front(); /*获得队列头部元素。此函数返回值为队列的头部元素,
常与pop_front()函数一起,先通过这个 
dq.front()获得队列头部元素,
然后用pop_front()将其从队列中删除;*/
dq.back(); //获得队列尾部元素。此函数返回值为队列的尾部元素,常与pop_back()函数一起,
//先通过back()获得队列头部元素,然后用pop_back()将其从队列中删除;
dq.size(); //获得队列大小。此函数返回队列的大小,返回值是“size_t”类型的数据,
//“size_t”是“unsigned int”的别名;
dq.empty(); //判断队列是否为空。此函数返回队列是否为空,返回值是bool类型。队列空
//:返回true;不空:返回false。

3. Deque example

Teacher Xiaoyi is very strict. It requires all students to line up before entering the classroom, and he requires students to be arranged in a non-decreasing order of height. Once, when n students were in line, Teacher Xiaoyi happened to go to the bathroom. The students finally had a chance to fight back, so the students decided to come to a crazy queue. They defined the crazy value of a queue as the absolute sum of the height difference of each pair of adjacent students. Since the madness value of the queue arranged in the order of height is the smallest, of course they decided to line up in the order of the largest madness value. Now given the height of n students, please calculate the maximum possible crazy value of these students in line. Teacher Xiao Yi will definitely be half angry when he comes back.

Enter description:

The input consists of two lines, the first line is an integer n (1 ≤ n ≤ 50), indicating the number of students, and the 
second line is n integers h(i] (1 ≤ h(i) ≤ 1000), indicating the height of each student

Output description:

Output an integer, representing the largest crazy value that n students can get in line. 

As shown in the example: 
when the order of the queue is: 25-10-40-5-25, the total absolute value of the height difference is 15+30+35+20=100. 
This is the biggest crazy value.

Example 1

enter

5
5 10 25 40 25

Output

100

Code:

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[50];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    deque<int> d;
    int l=0,r=n-1;
    d.push_front(a[r]);
    r--;
    while(l<=r){
         if(l<=r){
             d.push_front(a[l++]);
             if(l<=r)
             d.push_back(a[l++]);
         }
         if(l<=r){
              d.push_front(a[r--]);
              if(l<=r)
              d.push_back(a[r--]);
         }
    }
    if(abs(d[n-1]-d[n-2])<abs(d[n-1]-d[0])){
            d.push_front(d.back());
            d.pop_back();
    }
    int res=0;
    for(int i=1;i<n;i++){
        res+=abs(d[i]-d[i-1]);
    }
    cout<<res<<endl;
    return 0;
}

I spent three days writing it, if it is useful to you, please like it~

Guess you like

Origin blog.csdn.net/Keven_11/article/details/107844744