zoukankan      html  css  js  c++  java
  • poj 2299 Ultra-QuickSort (树状数组)

    Ultra-QuickSort
    Time Limit: 7000MS   Memory Limit: 65536K
    Total Submissions: 37971   Accepted: 13672

    Description

    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
    

    Source

     1 //Accepted    9356K    563MS    C++    1145B
     2 /*
     3 
     4     题意:
     5         给出一串数列,问用冒泡排序发要进行多少次交换。
     6     
     7     树状数组:  
     8         我的理解是这题和poj 2352类似,不过这题一定要加上离散化。
     9        
    10         这题就是要求每个元素中排在其前面且比它大的元素的数量的和。
    11         我转换成和之前那题一样的思想,由最大的数减去该数的排位,
    12     最后求出来的a[i]就是所求,表示前面有i个比它大的数。然后每个
    13     数都要交换i次,故要乘上i。 
    14      
    15 
    16 */
    17 #include<stdio.h>
    18 #include<stdlib.h>
    19 #include<iostream>
    20 #define N 500005
    21 using namespace std;
    22 struct node{
    23    int val;
    24    int pos;
    25 };
    26 node no[N];
    27 int b[N],a[N],c[N];
    28 int cmp(const void*a,const void*b)
    29 {
    30     return (*(node*)a).val-(*(node*)b).val;
    31 }
    32 inline int lowbit(int i)
    33 {
    34     return i&(-i);
    35 }
    36 void update(int k,int detal)
    37 {
    38     for(;k<N;k+=lowbit(k)) 
    39         c[k]+=detal;
    40 }
    41 inline int getsum(int k)
    42 {
    43     int t=0;
    44     for(;k>0;k-=lowbit(k)) 
    45         t+=c[k];
    46     return t; 
    47 }
    48 int main(void)
    49 {
    50     int n;
    51     while(scanf("%d",&n)!=EOF && n)
    52     {
    53         __int64 sum=0;
    54         for(int i=0;i<n;i++){
    55             scanf("%d",&no[i].val);
    56             no[i].pos=i;
    57         }
    58         qsort(no,n,sizeof(no[0]),cmp);
    59         for(int i=0;i<n;i++) b[no[i].pos]=i;
    60         //for(int i=0;i<n;i++) printf("%d
    ",b[i]);
    61         memset(c,0,sizeof(c));
    62         memset(a,0,sizeof(a));
    63         for(int i=0;i<n;i++){
    64             int t=N-b[i]-5;
    65             a[getsum(t)]++;
    66             update(t,1);
    67         }
    68         for(int i=1;i<n;i++) sum+=i*a[i];
    69         printf("%I64d
    ",sum);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    (easy)LeetCode 223.Rectangle Area
    (easy)LeetCode 205.Reverse Linked List
    (easy)LeetCode 205.Isomorphic Strings (*)
    (easy)LeetCode 204.Count Primes
    (easy)LeetCode 203.Remove Linked List Elements
    (easy)LeetCode 202.Happy Number
    (easy)LeetCode 198.House Robber
    (easy)LeetCode 191.Number of 1 Bits
    试题分析
    使用ADO.NET访问数据库
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3632458.html
Copyright © 2011-2022 走看看