• 【HDOJ】1908 Double Queue


    双端队列+二分。

     1 #include <cstdio>
     2 
     3 #define MAXN 1000005
     4 
     5 typedef struct {
     6     int id;
     7     int p;
     8 } node_st;
     9 
    10 node_st que[MAXN];
    11 int rear = 0,front = 0;
    12 
    13 void Insert(int id, int p) {
    14     int l = front, r = rear, mid;
    15 
    16     while ( l <= r) {
    17         mid = (l+r)>>1;
    18         if (que[mid].p == p)
    19             break;
    20         if (que[mid].p < p)
    21             l = mid+1;
    22         else
    23             r = mid - 1;
    24     }
    25     while (mid<rear && que[mid].p <= p)
    26         ++mid;
    27     for (int i=rear-1; i>=mid; --i)
    28         que[i+1] = que[i];
    29     que[mid].id = id;
    30     que[mid].p = p;
    31     rear++;
    32 }
    33 
    34 int main() {
    35     int c, id, p;
    36 
    37     while (scanf("%d", &c)!=EOF && c) {
    38         if (c == 1) {
    39             scanf("%d %d", &id, &p);
    40             Insert(id, p);
    41         } else {
    42             if (rear == front)
    43                 printf("0
    ");
    44             else {
    45                 if (c == 2)
    46                     printf("%d
    ", que[--rear].id);
    47                 else
    48                     printf("%d
    ", que[front++].id);
    49             }
    50         }
    51     }
    52 
    53     return 0;
    54 }
  • 相关阅读:
    python 编码与解码
    python 写文件
    python 文件读写
    python 异常处理
    python 断言
    C++的可移植性和跨平台开发
    Python中subprocess学习
    Python 的web自动化测试
    CookieJar和HTTPCookieProcessor
    python3爬虫
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3788403.html
Copyright © 2020-2023  润新知