zoukankan      html  css  js  c++  java
  • POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)

    题目链接

    ac代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<bitset>
    #include<cassert>
    #include<cctype>
    #include<cmath>
    #include<cstdlib>
    #include<ctime>
    #include<deque>
    #include<iomanip>
    #include<list>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define mp make_pair
    #define pi acos(-1)
    #define pii pair<int, int>
    #define pll pair<long long , long long>
    #define ll long long
    #define ld long double
    #define MEMS(x) memset(x, -1, sizeof(x))
    #define MEM(x) memset(x, 0, sizeof(x))
    const int inf = 0x3f3f3f3f;
    const int maxn = 500001;
    using namespace std;
    int n;
    struct Intg
    {
        int data;
        int pos;
        bool operator<(const Intg num) const
        {
            return data < num.data;
        }
    }a[maxn];
    int p[maxn], d[maxn];
    int lowbit(int x)
    {
        return x & (-x);
    }
    void update(int x)
    {
        while(x <= maxn)
        {
            d[x]++;
            x += lowbit(x);
        }
    }
    int getsum(int x)
    {
        int ans = 0;
        while(x)    //一定写成这种形式,否则写成for循环形式会导致超时
        {
            ans += d[x];
            x -= lowbit(x);
        }
        return ans;
    }
    int main()
    {
    //    freopen("input.txt", "r", stdin);
    //    freopen("output.txt", "w", stdout);
        while(scanf("%d", &n) != EOF && n)
        {
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", &a[i].data);
                a[i].pos = i;
            }
            sort(a + 1, a + n + 1);
            p[a[1].pos] = 1;
            int idx = 1;
            for(int i = 2; i <= n ; i++)
            {
                if(a[i].data == a[i-1].data)
                    p[a[i].pos] = idx;
                else
                    p[a[i].pos] = ++idx;
            }
            ll ans = 0;
            memset(d, 0, sizeof(d));
            for(int i = 1; i <= n; i++)
            {
                update(p[i]);
                ans += i - getsum(p[i]);
            }
            printf("%lld
    ", ans);
        }
    }
    
    
    
  • 相关阅读:
    线程池的爆掉
    WebApi的调用-1.前端调用
    使用SqlSugar 4.X的T4生成实体类
    JSON.NET 空值处理, 数字转字符,时间格式化
    C#获取路径
    MVC4 下DropDownList使用方法(转)
    14.并发与异步
    14.并发与异步
    14.并发与异步
    VS2013 生成时复制文件或目录到指定目录
  • 原文地址:https://www.cnblogs.com/KeepZ/p/12315307.html
Copyright © 2011-2022 走看看