poj 2299 Ultra-QuickSort (樹狀數組+離散化)

來源:互聯網
上載者:User

標籤:

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 48257   Accepted: 17610

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

59105431230

Sample Output

60

Source

Waterloo local 2005.02.05這道題可以總結的地方不少。1:對於一組亂序數列,每次只能交換相鄰元素,達到有序交換的次數就是原數列中你逆序對的個數。  cf上好像總喜歡出這個題。。。我印象中就出現三次了。。。。。2:原始數組a[i]和樹狀數組的t[i]的對應問題:
  這道題n是500000,如果直接開數組是可以開得下的,不需要離散化。但是樹狀數組的下標對應的是原始數組的值!也就是t[i]的下表最大可能為999,999,999 ! 顯然存不下,需要離散化。3:學習了離散化的又一種寫法。
 1 /************************************************************************* 2     > File Name: code/poj/2299.cpp 3     > Author: 111qqz 4     > Email: [email protected]  5     > Created Time: 2015年08月04日 星期二 12時27分32秒 6  ************************************************************************/ 7  8 #include<iostream> 9 #include<iomanip>10 #include<cstdio>11 #include<algorithm>12 #include<cmath>13 #include<cstring>14 #include<string>15 #include<map>16 #include<set>17 #include<queue>18 #include<vector>19 #include<stack>20 #define y0 abc111qqz21 #define y1 hust111qqz22 #define yn hez111qqz23 #define j1 cute111qqz24 #define tm crazy111qqz25 #define lr dying111qqz26 using namespace std;27 #define REP(i, n) for (int i=0;i<int(n);++i)  28 typedef long long LL;29 typedef unsigned long long ULL;30 const int inf = 0x7fffffff;31 const int N=5E5+7;32 int n;33 int t[N];34 int ref[N];35 36 struct Q37 {38     int val,id;39 }q[N];40 41 42 bool cmp(Q a,Q b)43 {44     if (a.val<b.val) return true;45     return false;46 }47 int lowbit( int x)48 {49     return x&(-x);50 }51 void update ( int x,int c)52 {53     for ( int i = x ; i < N ; i = i + lowbit(i))54     {55     t[i] = t[i] + c;56     }57 }58 LL Sum( int x)59 {60     LL res = 0;61     for ( int i = x; i >= 1 ; i = i - lowbit(i))62     {63     res = res + t[i];64     }65     return res;66 }67 int main()68 {69     while (~scanf("%d",&n)&&n)70     {71     memset(t,0,sizeof(t));72     for ( int i = 1 ; i  <= n ; i++ )73     {74         scanf("%d",&q[i].val);  //離散化的時候相對大小不能打亂75         q[i].id = i;76     }77     sort(q+1,q+n+1,cmp);78     for ( int i = 1 ; i <= n ; i++ )79     {80         ref[q[i].id] = i;81     }82     LL ans = 0;83     for ( int i = 1 ; i <= n ; i++ )84     {85         update(ref[i],1);86         ans = ans + i-Sum(ref[i]);  87       //  cout<<"ans:"<<ans<<endl;88         89     }90     cout<<ans<<endl;91     }92     return 0;93 }

 

poj 2299 Ultra-QuickSort (樹狀數組+離散化)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.