• HDU6375双端队列


    要点分析:

    1.本题可以使用C++STL中的deque双端队列来方便解决(底层是一个双向的链表)

    2.值得注意的是N的上限为150000,所以直接开这么大的空间会超内存,可以配合map一起使用

    关于双端队列的声明:

    使用<queue>头文件

    主要用法:

    deque.push_back(val);

    deque.push_front(val);

    deque.pop_front();

    deque.pop_back();

    deque.front();

    deque.back();

    deque.clear();

    本题代码:

     1 #include<iostream>
     2 #include<queue>
     3 #include<map>
     4 using namespace std;
     5 
     6 //const int N = 150005;
     7 map<int, deque<int> > q;
     8 
     9 void read(int &x){
    10     char ch = getchar();x = 0;
    11     for (; ch < '0' || ch > '9'; ch = getchar());
    12     for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    13 }
    14 
    15 int main(){
    16     int N, Q;
    17     while(scanf("%d%d", &N, &Q) != EOF){
    18         for(int i = 1; i <= N; i++) q[i].clear();
    19         for(int i = 1; i <= Q; i++){
    20             int x;
    21             read(x);
    22             if(x == 1){
    23                 int u, w, val;
    24                 read(u);
    25                 read(w);
    26                 read(val);
    27                 if(w == 0){
    28                     q[u].push_front(val);
    29                 }else{
    30                     q[u].push_back(val);
    31                 }
    32             }else if(x == 2){
    33                 int u, w;
    34                 read(u);
    35                 read(w);
    36                 if(q[u].empty()){
    37                     printf("-1
    ");
    38                 }else{
    39                     if(w == 0){
    40                         int temp = q[u].front();
    41                         q[u].pop_front();
    42                         printf("%d
    ", temp);
    43                     }else{
    44                         int temp = q[u].back();
    45                         q[u].pop_back();
    46                         printf("%d
    ", temp);
    47                     }
    48                 }
    49             }else{
    50                 int u, v, w;
    51                 read(u);
    52                 read(v);
    53                 read(w);
    54                 if(w == 0){
    55                     while(!q[v].empty()){
    56                         int temp = q[v].front();
    57                         q[v].pop_front();
    58                         q[u].push_back(temp);
    59                     }
    60                 }else{
    61                     while(!q[v].empty()){
    62                         int temp = q[v].back();
    63                         q[v].pop_back();
    64                         q[u].push_back(temp);
    65                     }
    66                 }
    67             }
    68         }
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    今天查看了java文档中的sort方法
    记录下git简单使用(以码云为例)
    今天是leetcode300
    今天了解了一下摩尔投票法
    # 今天学习了一下java8的lambda表达式
    make命令的-j -f参数说明
    shell中单引号和双引号区别
    判断字符串相似度
    shell计算时间差
    hive cli转hive beeline的几个例子
  • 原文地址:https://www.cnblogs.com/findview/p/12152128.html
Copyright © 2020-2023  润新知