zoukankan      html  css  js  c++  java
  • stl(set或map)

    https://nanti.jisuanke.com/t/41384

    There are nnn points in an array with index from 111 to nnn, and there are two operations to those points.

    1: 1 x1 x1 x marking the point xxx is not available

    2: 2 x2 x2 x query for the index of the first available point after that point (including xxx itself) .

    Input

    nqnquad qnq

    z1x1z_1 quad x_1z1x1

    ⋮vdots

    zqxqz_qquad x_qzqxq

    qqq is the number of queries, zzz is the type of operations, and xxx is the index of operations. 1≤x<n<1091≤x<n<10^91x<n<109, 1≤q<1061 leq q<10^6 1q<106 and zzz is 111 or 222

    Output

    Output the answer for each query.

    样例输入

    5 3
    1 2
    2 2
    2 1

    样例输出

    3
    1

    题意:给出1-n,q个操作
    第一个操作(1):将某个值标记为不可用;
    第二个操作(2):输出该值以后的第一个有用值。
    神奇的编译器:c++14超时,c++11就过辽。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <set>
    using namespace std;
    
    #define mem(a) memset(a,0,sizeof(a))
    #define ll long long
    const int maxn = 100100;
    int n,q,a,b;
    set<int>s;
    int solve()
    {
        while(b <= n)
        {
            if(s.find(b) != s.end())
                b++;
            else return b;
        }
        return -1;
    }
    
    int main()
    {
        scanf("%d%d",&n,&q);
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d",&a,&b);
            if(a == 1)
            {
                s.insert(b);
            }
            else
            {
                printf("%d
    ",solve());
                // 也可以s.count判断是否存在。
                /*while(s.count(b))
                {
                    b++;
                }
                printf("%d
    " , b);*/
    
            }
        }
        return 0;
    }

    map会超时要用unordered_map
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <set>
    #include <unordered_map>
    using namespace std;
    
    #define mem(a) memset(a,0,sizeof(a))
    #define ll long long
    const int maxn = 100100;
    int n,q,a,b;
    
    int main()
    {
        unordered_map<int , int>mp;
        scanf("%d%d",&n,&q);
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d",&a,&b);
            if(a == 1)
            {
                mp[b] = -1 ;
            }
            else
            {
                while(mp[b] == -1) b++ ;
                printf("%d
    " , b);
            }
        }
        return 0;
    }


  • 相关阅读:
    SpringBoot08-缓存
    Spring注解-自动装配(三)
    Spring注解-生命周期与属性赋值(二)
    Spring注解-组件注册(一)
    剖析SpringMVC流程与整合(八)
    SpringMVC视图解析与配置(六)
    SpringMVC的其他功能(七)
    简单了解SpringMVC(五)
    Java动态代理
    Spring事务(四)
  • 原文地址:https://www.cnblogs.com/nonames/p/11488306.html
Copyright © 2011-2022 走看看