zoukankan      html  css  js  c++  java
  • poj--2299(树状数组+离散化)

    一、离散化:

    https://www.cnblogs.com/2018zxy/p/10104393.html

    二、逆序数

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn = 510000;
    struct Node{
        LL data;
        int val;
    }cur[maxn];
    LL a[maxn];
    bool cmp(Node A,Node B)
    {
        return A.data<B.data;
    }
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int x)
    {
        while(x<maxn-10)
        {
            a[x]++;
            x+=lowbit(x);
        }
    }
    int sum(int x)
    {
        int ans=0;
        while(x>0)
        {
            ans+=a[x];
            x-=lowbit(x);
        }
        return ans;
    }
    int main(void)
    {
        int n,i;
        while(~scanf("%d",&n)&&n)
        {
            memset(a,0,sizeof(a));
            for(i=1;i<=n;i++)
            {
                scanf("%lld",&cur[i].data);cur[i].val=i;
            }
            sort(cur+1,cur+1+n,cmp);
            LL ans=0;
            for(i=1;i<=n;i++)
            {
                update(cur[i].val);
                ans+=i-sum(cur[i].val);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn = 510000;
    struct Node{
        LL data;
        int val;
    }cur[maxn];
    LL a[maxn];
    bool cmp(Node A,Node B)
    {
        return A.data<B.data;
    }
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int x)
    {
        while(x<maxn-10)
        {
            a[x]++;
            x+=lowbit(x);
        }
    }
    int sum(int x)
    {
        int ans=0;
        while(x>0)
        {
            ans+=a[x];
            x-=lowbit(x);
        }
        return ans;
    }
    int main(void)
    {
        int n,i;
        while(~scanf("%d",&n)&&n)
        {
            memset(a,0,sizeof(a));
            for(i=1;i<=n;i++)
            {
                scanf("%lld",&cur[i].data);cur[i].val=i;
            }
            sort(cur+1,cur+1+n,cmp);
            LL ans=0;
            for(i=n;i>=1;i--)
            {
                ans+=sum(cur[i].val);
                update(cur[i].val);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    HDU 1010 Tempter of the Bone
    HDU 4421 Bit Magic(奇葩式解法)
    HDU 2614 Beat 深搜DFS
    HDU 1495 非常可乐 BFS 搜索
    Road to Cinema
    Sea Battle
    Interview with Oleg
    Spotlights
    Substring
    Dominating Patterns
  • 原文地址:https://www.cnblogs.com/2018zxy/p/10104279.html
Copyright © 2011-2022 走看看