• 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
  • 相关阅读:
    Java虚拟机详解(二)------运行时内存结构
    Java虚拟机简介
    JAVA 用数组实现 ArrayList
    Java 集合详解
    Spring+SpringMVC+Mybatis框架集成搭建教程
    Java代理
    单例模式(三)
    Node.js安装及环境配置
    Java 并发编程:核心理论(一)
    Java 并发编程:volatile的使用及其原理(二)
  • 原文地址:https://www.cnblogs.com/2018zxy/p/10104279.html
Copyright © 2020-2023  润新知