zoukankan      html  css  js  c++  java
  • Ultra-QuickSort POJ

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
    9 1 0 5 4 ,

    Ultra-QuickSort produces the output 
    0 1 4 5 9 .

    Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

    Input

    The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

    Output

    For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

    Sample Input

    5
    9
    1
    0
    5
    4
    3
    1
    2
    3
    0
    

    Sample Output

    6
    0

    简单的树状数组应该 应该是最基本的应用求逆序对
    注意离散化
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <set>
     7 #include <iostream>
     8 #include <map>
     9 #include <stack>
    10 #include <string>
    11 #include <vector>
    12 #define pi acos(-1.0)
    13 #define eps 1e-6
    14 #define fi first
    15 #define se second
    16 #define lson l,m,rt<<1
    17 #define rson m+1,r,rt<<1|1
    18 #define bug         printf("******
    ")
    19 #define mem(a,b)    memset(a,b,sizeof(a))
    20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
    21 #define f(a)        a*a
    22 #define sf(n)       scanf("%d", &n)
    23 #define sff(a,b)    scanf("%d %d", &a, &b)
    24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
    25 #define pf          printf
    26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
    27 #define FREE(i,a,b) for(i = a; i >= b; i--)
    28 #define FRL(i,a,b)  for(i = a; i < b; i++)
    29 #define FRLL(i,a,b) for(i = a; i > b; i--)
    30 #define FIN freopen("DATA.txt","r",stdin)
    31 #define lowbit(x)   x&-x
    32 #pragma comment (linker,"/STACK:102400000,102400000")
    33 
    34 using namespace std;
    35 typedef long long LL ;
    36 const int maxn = 5e5 + 10;
    37 int n, a[maxn], c[maxn], b[maxn], bit[maxn];
    38 void update(int x) {
    39     while(x <= n) {
    40         bit[x] += 1;
    41         x += lowbit(x);
    42     }
    43 }
    44 int sum(int i) {
    45     int ret = 0;
    46     while(i > 0) {
    47         ret += bit[i];
    48         i -= lowbit(i);
    49     }
    50     return ret;
    51 }
    52 int main() {
    53     while(scanf("%d", &n), n) {
    54         for (int i = 1 ; i <= n ; i++) {
    55             scanf("%d", &a[i]);
    56             b[i] = a[i];
    57         }
    58         sort(b + 1, b + 1 + n);
    59         for (int i = 1 ; i <= n ; i++)
    60             c[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
    61         mem(bit, 0);
    62         LL ans=0;
    63         for (int i=1 ;i<=n ;i++) {
    64             update(c[i]);
    65             ans+=i-sum(c[i]);
    66         }
    67         printf("%lld
    ",ans);
    68     }
    69     return 0;
    70 }


  • 相关阅读:
    计算机体系结构的铁律(iron law)
    PHP 画图——使用jpgraph画图
    理解Paxos Made Practical
    【bzoj1015】【JSOI2008】【星球大战】【并查集+离线】
    Spark调研笔记第3篇
    hduoj2094产生冠军
    使用HD/IDE层的ioctl接口获取磁盘容量get_hdd_max_sector
    给GridView设置行高
    tomcat的一些简单配置
    【JavaScript】--JavaScript总结一览无余
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9415276.html
Copyright © 2011-2022 走看看