hdu 2561 第二小整數 (優先隊列)

使用優先隊列,只維持隊列中只有兩個元素,對輸入的數據做處理,只留下比當前top()小的數據其他的扔掉,然後輸出top()即可

#include<stdio.h>
#include<math.h>
#include <queue>
#include<algorithm>
#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        priority_queue < int >q;
        int n;
        cin>>n;
        while(n--)
        {
            int num;
            scanf("%d",&num);
            if(q.empty())q.push(num);
            else
            {
                if(q.size()<2)q.push(num);
                else
                {
                    if(num<q.top())
                    {
                        q.push(num);
                        q.pop();
                    }
                }
            }
        }
        printf("%d\n",q.top());
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章