• POJ 2352 Stars


    题意:有一堆星星,每个星星的级别为坐标不高于它且不在它右边的星星个数,求级别为0~n - 1的星星个数。

    解法:树状数组。输入的星星坐标已经按y坐标升序排序,y坐标相等的按x升序排序,所以每输入一个数只要看之前输入的星星里有几个x坐标小于等于它的x坐标即为它的等级,等级计数器加一,把这个星星的x坐标加入树状数组,最后扫一遍等级计数器输出。并没注意到x坐标有0这种坑爹的事情……果断T了……

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    int BA[32005];
    int n;
    inline int lowbit(int x)
    {
        return x & (-x);
    }
    void update(int pos)
    {
        for(int i = pos; i <= 32005; i += lowbit(i))
            BA[i]++;
    }
    int query(int pos)
    {
        int res = 0;
        for(int i = pos; i > 0; i -= lowbit(i))
            res += BA[i];
        return res;
    }
    int main()
    {
        while(~scanf("%d", &n))
        {
            memset(BA, 0, sizeof BA);
            int ans[15005] = {0};
            for(int i = 0; i < n; i++)
            {
                int x, y;
                scanf("%d%d", &x, &y);
                x++;
                ans[query(x)]++;
                //cout << "q = " << query(x) << endl;
                update(x);
            }
            for(int i = 0; i < n; i++)
                printf("%d
    ", ans[i]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    python 包与模块
    互斥锁与自旋锁
    TCP三次握手四次挥手
    缓存击穿、缓存穿透、缓存雪崩
    五种IO模型
    MySQL使用mysqldump进行数据备份
    golang数据库连接池参数设置
    golang代码文件目录组织、包目录组织学习笔记
    【转】如何用Vim提高开发效率
    emacs显示行号
  • 原文地址:https://www.cnblogs.com/Apro/p/4504234.html
Copyright © 2020-2023  润新知