zoukankan      html  css  js  c++  java
  • NCPC 2012 Cookie Selection

    题目要求每次输出中间的那个数,如果数据很大肯定扛不住;

    所以用两个优先队列来维护;

    这样的话中间的那个数反正会在两个队列的任何一个的头部;

    时间复杂度肯定比较小;

    代码:

     1 #include <cstdio>
     2 #include <queue>
     3 using namespace std;
     4 int l1,l2;
     5 priority_queue<int> q1;
     6 priority_queue<int, vector<int>, greater<int> > q2;
     7 void add(int x)
     8 {
     9     q2.push(x);
    10     l2++;
    11     if(!q1.empty() && !q2.empty())
    12     {
    13         while(q1.top()>q2.top())
    14         {
    15             x=q1.top();
    16             q1.pop();
    17             q1.push(q2.top());
    18             q2.pop();
    19             q2.push(x);
    20         }
    21     }
    22     if(q1.size()<q2.size())
    23     {
    24         x=q2.top();
    25         q1.push(x);
    26         q2.pop();
    27         l1++;
    28         l2--;
    29     }
    30 }
    31 int get()
    32 {
    33     int ret;
    34     if(l1>l2)
    35     {
    36         l1--;
    37         ret=q1.top();
    38         q1.pop();
    39     }
    40     else
    41     {
    42         l2--;
    43         ret=q2.top();
    44         q2.pop();
    45     }
    46     return ret;
    47 }
    48 
    49 int main()
    50 {
    51     char str[15];
    52     int x;
    53     while(scanf("%s",str)!=EOF)
    54     {
    55         if(str[0]=='#')
    56             printf("%d
    ",get());
    57         else
    58         {
    59             sscanf(str,"%d",&x);
    60             add(x);
    61         }
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    Activity 生命周期 返回键 退出 杂谈
    多线程基本语法
    常用代码
    JSP 相关
    powerDesiger uml class
    抽象类的说明
    javaScript 中常用的正则表达式
    chickbox 的使用
    对象在内存中的状态
    jQuery 常用代码
  • 原文地址:https://www.cnblogs.com/yours1103/p/3389916.html
Copyright © 2011-2022 走看看