HDU 2275 Kiki & Little Kiki 1

$map$,$set$。

有可能有多个相同的数字,所以开一个$map$记录一下每个数字分别有几个,当$map[x]$减小到为$0$时,将$x$从$set$中删去。

对于每一次询问,直接$lower$_$bound$找到第一个大于等于询问数字的地方,如果是$s.end()$,那么就输出上一个位置,如果值刚好等于询问的数字,那么直接输出。否则,也是输出上一个位置。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
}

int n;

int main()
{
    while(~scanf("%d",&n))
    {
        set<int>s;
        map<int,int>m;

        for(int i=1;i<=n;i++)
        {
            char op[10]; int x;
            scanf("%s%d",op,&x);

            if(op[1]=='u')
            {
                if(m[x]==0) s.insert(x);
                m[x]++;
            }
            else
            {
                int xx=*s.begin();
                if(s.size()==0) printf("No Element!\n");
                else if(xx>x) printf("No Element!\n");
                else
                {
                    set<int>::iterator iter;
                    iter = s.lower_bound(x);

                    if(iter==s.end())
                    {
                        iter--;
                        int y=*iter;
                        m[y]--;
                        if(m[y]==0) s.erase(y);
                        printf("%d\n",y);
                    }
                    else if(*iter==x)
                    {
                        int y=*iter;
                        m[y]--;
                        if(m[y]==0) s.erase(y);
                        printf("%d\n",y);
                    }
                    else
                    {
                        iter--;
                        int y=*iter;
                        m[y]--;
                        if(m[y]==0) s.erase(y);
                        printf("%d\n",y);
                    }
                }
            }
        }

        printf("\n");
    }
    return 0;
}

 

posted @ 2017-01-17 18:50  Fighting_Heart  阅读(208)  评论(0编辑  收藏  举报