zoukankan      html  css  js  c++  java
  • POJ-2481-Cows

    链接:https://vjudge.net/problem/POJ-2481

    题意:

    有n头牛,每头牛有一个范围,s-e,当一头牛的s大于等于另一头牛和e小于等于另一头牛同时e-s也小于另一头牛

    说明另一头牛比这头牛强壮。

    求这n头牛每头牛有几个比他强壮。

    思路:

    树状数组。

    先按照e从大到小排序,保证后面的牛的e小于等于前面一头牛,

    同时树状数组记录每个s对应的值,根据前面有几个比他小的s来判断比他强壮的牛的个数。

    代码:

    #include <iostream>
    #include <memory.h>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <queue>
    #include <string>
    #include <stack>
    #include <iterator>
    #include <stdlib.h>
    #include <time.h>
    #include <assert.h>
    
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 5e5+10;
    int c[MAXN],res[MAXN];
    int n;
    
    struct Node
    {
        int s, e;
        int pos;
        bool operator < (const Node & that)const
        {
            if (this->e != that.e)
                return this->e > that.e;
            return this->s < that.s;
        }
    }node[MAXN];
    
    int Lowbit(int k)
    {
        return k&(-k);
    }
    
    void Add(int x, int v)
    {
        while (x <= MAXN)
        {
            c[x] += v;
            x += Lowbit(x);
        }
    }
    
    int Sum(int x)
    {
        int res = 0;
        while (x > 0)
        {
            res += c[x];
            x -= Lowbit(x);
        }
        return res;
    }
    
    int main()
    {
        while(~scanf("%d", &n) && n)
        {
            memset(c, 0, sizeof(c));
            for (int i = 1;i <= n;i++)
            {
                scanf("%d%d", &node[i].s, &node[i].e);
                node[i].pos = i;
            }
            sort(node+1, node+1+n);
            res[node[1].pos] = 0;
            Add(node[1].s+1, 1);
            for (int i = 2;i <= n;i++)
            {
                if (node[i].e == node[i-1].e && node[i].s == node[i-1].s)
                    res[node[i].pos] = res[node[i-1].pos];
                else
                    res[node[i].pos] = Sum(node[i].s+1);
                Add(node[i].s+1, 1);
            }
            for (int i = 1;i <= n;i++)
                printf("%d ", res[i]);
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    LeetCode344. 反转字符串
    LeetCode59. 螺旋矩阵 II
    LeetCode209. 长度最小的子数组
    LeetCode383. 赎金信
    cannot be cast to javax.servlet.Servlet
    求组合
    数据库系统概论王珊第四版 答案
    数据库复习
    dfs-bfs
    dfs
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10688993.html
Copyright © 2011-2022 走看看