• FZU Problem 2029 买票问题(树状数组)


      当我看到题目是5秒的时候,压根没有想树状数组,一直奔着模拟队列去想了,最后也没搞定,赛后看到大神的题解才恍然大悟,原来如此,题目中有明显的暗示,求前n项和,骤然感叹,自己太low...

    果然还是要多做多研究啊,下面是代码,还有一处脑残错误在代码里...当时真的是找了好久才发现...

    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <cstring>
    using namespace std;
    #define maxn 100005
    int c[maxn],mark[maxn],n;
    int lowbit(int k)
    {
        return k&(-k);
    }
    void update(int x,int v)///把树状数组的上界写了变量num,wa了无数次
    {
        while(x <= n)
        {
            c[x] += v;
            x += lowbit(x);
        }
    }
    int cal(int x)
    {
        int ans = 0;
        while(x > 0)
        {
            ans += c[x];
            x -= lowbit(x);
        }
        return ans;
    }
    struct Node
    {
        int n,y;
        friend bool operator < (Node a,Node b)
        {
            return a.y > b.y;
        }
    };
    int main()
    {
        int t,top,num,x,y;
        Node tmp;
        char op[20];
        while(~scanf("%d",&t))
        {
            n = t;
            top = 1,num = 1;
            memset(c,0,sizeof(c));
            memset(mark,0,sizeof(mark));
            map<int,int> pos;
            priority_queue<Node>que;
            while(!que.empty()) que.pop();
            while(t--)
            {
                scanf("%s",op);
                if(op[0] == 'a')
                {
                    scanf("%d%d",&x,&y);
                    tmp.n = num,tmp.y = y;
                    que.push(tmp);
                    pos[x] = num;
                    mark[num] = 1;
                    num++;
                }
                else if(op[0] == 'p')
                {
                    while(mark[top] == 0 && top <= num) top++;
                    if(top > num)continue;
                    mark[top] = 0;
                    update(top,-1);
                }
                else if(op[0] == 'l')
                {
                    while(!que.empty() && mark[que.top().n] == 0)
                    {
                        que.pop();
                    }
                    if(!que.empty())
                    {
                        Node now;
                        now = que.top();
                        que.pop();
                        mark[now.n] = 0;
                        update(now.n,-1);
                    }
                }
                else if(op[0] == 'c')
                {
                    scanf("%d%d",&x,&y);
                    int m = pos[x];
                    if(pos[x] == 0) continue;
                    if(mark[m] == 0) continue;
                    int tot = m - 1 + cal(pos[x]-1);
                    printf("%d
    ",tot);
                    if(tot > y)
                    {
                        mark[m] = 0;
                        update(m,-1);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Boost练习程序(强制转换)
    4873279(1002)
    A+B Problem(1000)
    STL练习程序(去除相同元素)
    Boost练习程序(智能指针)
    Sql技巧总结
    MySql Show Status详解
    mysql show status调优
    mysql decimal、numeric数据类型
    Apache Thrift学习小记
  • 原文地址:https://www.cnblogs.com/jifahu/p/5448847.html
Copyright © 2020-2023  润新知