POJ 1442 Black Box(優先隊列)

來源:互聯網
上載者:User

標籤:優先隊列   acm   c語言   編程   演算法   

題目地址:POJ 1442

這題是用了兩個優先隊列,其中一個是較大優先,另一個是較小優先。讓較大優先的隊列保持k個。每次輸出較大優先隊列的隊頭。

每次取出一個數之後,都要先進行判斷,如果這個數比較大優先的隊列的隊頭要小,就讓它加入這個隊列,隊列頭移到較小優先的隊列中。然後當較大優先的數不足k個的時候,就讓較小優先的隊列的隊頭移到較大優先的隊頭中。

代碼如下;

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int a[40000], b[40000];struct xiao{    int x;    bool operator < (const xiao &a) const    {        return x > a.x;    }};struct da{    int x;    bool operator < (const da &a) const    {        return x<a.x;    }};int main(){    int n, m, i, j, cnt, x, k;    while(scanf("%d%d",&n,&m)!=EOF)    {        priority_queue<xiao>q1;        priority_queue<da>q2;        xiao f1;        da f2;        for(i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        for(i=0; i<m; i++)        {            scanf("%d",&b[i]);        }        j=0;        for(i=0; i<m; i++)        {            while(j<b[i])            {                if(q2.empty()||a[j]>=q2.top().x)                {                    f1.x=a[j];                    q1.push(f1);                }                else                {                    f1.x=q2.top().x;                    q1.push(f1);                    q2.pop();                    f2.x=a[j];                    q2.push(f2);                }                j++;            }            while(q2.size()<=i)            {                f1=q1.top();                f2.x=f1.x;                q1.pop();                q2.push(f2);            }            printf("%d\n",q2.top());        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.