• 队列c++


    今天是2019.7.22,距离NOIP2019还有109天

    高二下学期期末考试上周结束啦

    今天回归机房的第三天

    前两天听了兔哥讲的图论和数论

    感觉目标是NOIP的话好多知识其实并不太用得上

    比如FFT,二分图匹配,扩展CRT······
    今天没课,写点往年的NOIP原题吧

    先是这道P1540机器翻译

    传送门

    很容易发现这是一道队列+模拟的题

    不过队列的知识我已经忘得差不多了

    现在复习一下

    队列是一种只能从头进从尾出的线性数据结构

    头文件:#include<queue>

    常用的函数有pop、push、front、size、empty、back······

    定义一个队列格式为queue<···> ?(···表示数据类型 比如int   ?表示变量名 是自己随意取的)

    ex:queue<int> q; 即定义一个int类型的队列q

    这里提醒一下定义队列的时候无需说明大小,queue是动态分配空间的

    (不过不要认为空间是无穷大的)

    1.push

    push能将一个元素压入队首

    ex:

    queue<int> q;
    q.push(5);

    这里将数字5压进队首

    2.pop

    pop函数能将最先入队的元素弹出

    ex:

    queue<int> q;
    q.push(5);
    q.pop();

    这里将队中唯一元素5弹出

    3.empty

    empty函数是判断队列是否为空

    空返回1,非空返回0

    ex:

    queue<int> q;
    printf("%d ", q.empty()):
    q.push(5);
    printf("%d ", q.empty()):
    q.pop();
    printf("%d ", q.empty()):

    三次依次输出1 0 1

    4.front

    front函数返回的是最先进队的元素(即队尾元素)

    ex:

    queue<int> q;
    q.push(5);
    q.push(6)
    printf("%d
    ", q.front());

    输出的即为5

    5.size

    size函数返回的是队中元素个数(int)

    ex:

    queue<int> q;
    printf("%d "p.size());
    q.push(5);
    printf("%d "p.size());
    q.push(6);
    printf("%d "p.size());

    输出即为0 1 2

    6.back

    back函数返回的是最晚进队的元素(即队首元素)

    ex:

    queue<int> q;
    q.push(5);
    q.push(6);
    printf("%d
    ", q.back());

    输出的即为6

    注:写queue的函数时千万不要忘了括号(血的教训)

    有了上面这些知识,现在我们再来做这道P1540

    现在我们按照题意所述的模拟即可

    代码:

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    #include<cstdlib>
    #include<cmath>
    #include<string>
    #include<iostream>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #define enter puts("")
    #define space putchar(' ')
    using namespace std;
    typedef long long ll;
    int read()
    {
        int ans = 0, op = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-') op = 0;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            ans *= 10;
            ans += ch - '0';
            ch = getchar();
        }
        return op ? ans : -ans;
    } 
    void write(int x)
    {
        if(x < 0)
        {
            x = -x;
            putchar('-');
        }
        if(x >= 10) write(x / 10);
        putchar(x % 10 + '0');
    }
    queue<int> q;
    int n, m, a[1005], b[1005], ans;
    int main()
    {
        n = read(), m = read();
        for(int i = 1;i <= m;i++) a[i] = read();
        for(int i = 1;i <= m;i++)
        {
            if(b[a[i]] == 0)
            {
                if(q.size() < n)
                {
                    q.push(a[i]);
                    ans++;
                    b[a[i]] = 1;
                }
                else
                {
                    b[q.front()] = 0;
                    q.pop();
                    q.push(a[i]);
                    ans++;
                    b[a[i]] = 1;
                }
            }
        }
        write(ans);
        enter;
        return 0;
    }
  • 相关阅读:
    C# Redis实战(四)
    C# Redis实战(三)
    C# Redis实战(二)
    C# Redis实战(一)
    C#连接内存数据库redis【1、Redis存读取数据】
    C#连接内存数据库redis【1、安装配置】
    c#根据配置文件反射
    内存数据库:Redis与Memcached的区别
    内存数据库:memcached与redis技术的对比试验
    【转】【涨姿势】支付宝怎么做风险控制?
  • 原文地址:https://www.cnblogs.com/thx666/p/11224416.html
Copyright © 2020-2023  润新知