POJ 3481 Double Queue STLmap和set新学到的一点用法

2013-08-08

POJ 3481  Double Queue

这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是因为它又可能删除优先权最大的,又可能删除优先权最小的,所以当输入为2或者3的时候没办法判断是不是没有顾客了。通过这道题发觉map的其他用法真的是一点不会。所以看了别人的代码用了两种方法试着敲一下,写这个随笔是博客的第一篇文章,内容虽然这么水,但是的确是我之前掌握不好的部分,本菜鸟今天比赛之后受刺激了突然茅塞顿开,决定开此博客记录我的成长,就算小小的收获也晒出来吧,可能我之前缺乏的就是承认自己不会的本来就是很简单的东西的勇气,但愿若干年后我能发现自己坚持了好久。

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
struct node
{
    int num,v;
    node(){};
    node(int a,int b):num(a),v(b){}
    friend bool operator<(node a,node b){
        return a.v<b.v;
    }
};
set<node> st;
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        if(n==1)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            st.insert(node(a,b));
        }
        if(n==2)
        {
            if(st.size()==0) puts("0");
            else
            {
                set<node>::iterator it=st.end();
                it--;
                printf("%d\n",(*it).num);
                st.erase(it);
            }
        }
        if(n==3)
        {
            if(st.size()==0) puts("0");
            else
            {
                set<node>::iterator it=st.begin();
                printf("%d\n",(*it).num);
                st.erase(it);
            }
        }
    }
    return 0;
}
View Code set版本
#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>mp;
int main()
{
    int n;
    mp.clear();
    while(1)
    {
        scanf("%d",&n);
        if(!n) break;
        if(n==1)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            mp[b]=a;
        }
        if(n==2)
        {
            if(mp.size()==0) puts("0");
            else
            {
                map<int,int>::iterator it=mp.end();
                it--;
                printf("%d\n",it->second);
                mp.erase(it);
            }
        }
        if(n==3)
        {
            if(mp.size()==0) puts("0");
            else
            {
                map<int,int>::iterator it=mp.begin();
                printf("%d\n",it->second);
                mp.erase(it);
            }
        }
    }
    return 0;
}
View Code map版本

 

posted on 2013-08-08 00:32  Amo.  阅读(510)  评论(0)    收藏  举报

导航