POJ 2299 Ultra-quicksort

Source: Internet
Author: User

Topic Connection

http://poj.org/problem?id=2299

Ultra-quicksortdescription

In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, 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 what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.

Input

The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of 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 is processed.

Output

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

Sampleinput

5
9
1
0
5
4
3
1
2
3
0

Sampleoutput

6

0

Use line tree to find the number of reverse order.
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <map>8#include <Set>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingSTD::Set; the usingStd::map; - usingstd::p air; - usingstd::vector; - usingStd::multiset; + usingStd::multimap; - #defineAll (c) (c). Begin (), (c). End () + #defineITER (c) Decltype ((c). Begin ()) A #defineCLS (arr,val) memset (arr,val,sizeof (arr)) at #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - #definePB (E) push_back (e) - #defineMP (A, b) Make_pair (A, B) - #defineLC (ROOT&LT;&LT;1) in #defineRC (root<<1|1) - #defineMid ((l+r) >>1) to Const intMax_n =500100; +typedef unsignedLong Longull; - structNode {intVal;}; the structP { *     intV, id; $FriendBOOL operator< (ConstP &a,ConstP &b) {Panax Notoginseng         returnA.V = = B.V? a.ID < B.ID:A.V <B.V; -     } the }rec[max_n]; + structSegtree { ANode Seg[max_n <<2]; theInlinevoidinit () { +CLS (SEG,0); -     } $InlinevoidInsertintRootintLintRintp) { $         if(P > R | | p < l)return; -         if(P <= L && p >= R) {seg[root].val++;return; } - Insert (LC, L, Mid, p); theInsert (RC, Mid +1, R, p); -Seg[root].val = Seg[lc].val +Seg[rc].val;Wuyi     } theInline ull query (intRootintLintRintXinty) { -         if(X > R | | y < L)return 0; Wu         if(x <= l && y >= R)returnSeg[root].val; -ull ret =0; AboutRET + =Query (LC, L, Mid, X, y); $RET + = query (RC, Mid +1, R, X, y); -         returnret; -     } - }seg; A intMain () { + #ifdef LOCAL theFreopen ("In.txt","R", stdin); -Freopen ("OUT.txt","w+", stdout); $ #endif the     intN; the      while(~SCANF ("%d", &n) &&N) { the seg.init (); theull res =0; -          for(inti =1; I <= N; i++) scanf ("%d", &rec[i].v), rec[i].id =i; inSort (rec +1, REC + n +1); the          for(inti =1; I <= N; i++) { theRes + = Seg.query (1,1, N, Rec[i].id, n); AboutSeg.insert (1,1, N, rec[i].id); the         } theprintf"%lld\n", res); the     } +     return 0; -}
View Code

POJ 2299 Ultra-quicksort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.