Summer Camp (2)-----frosh Week (UVA11858)

Source: Internet
Author: User

H-frosh Week

crawling in process ... crawling failed time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

Description

During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such-game, all the Frosh-a team stand in a line, and is then asked to arrange themselves according to some crit Erion, such as their height, their birth date, or their student number. This rearrangement of the line must is accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.

Input

The first line of input contains one positive an integer n, the number of students on the team, which'll be is no more than on E million. The following n lines each contain one integer and the student number of each student on the team. No student number would appear more than once.

Output

Output a line containing the minimum number of swaps required to arrange the students in increasing order by student Numbe R.

Sample Input

3 3 1 2

Sample Output

2 Problem Analysis: Obviously this is a question of reverse order number, can be solved by merging sort.
1#include"Cstdio"2 3 intnum[1000005];4 intt[1000005];5 __int64 sum;6 voidMsetintN)7 {8    for(intI=1; i<=n;i++)9scanf ("%d",&num[i]);Ten } One voidpxintXinty) A { -    if(Y-x >1) -    { the        intm = x + (y-x)/2; -        intp = x,q = M,i =x; - px (x,m); - px (m,y); +         while(P < m | | Q <y) -        { +            if(q >= y | | (P < m && Num[p] <=Num[q])) At[i++] = num[p++]; at            Else -            { -t[i++] = num[q++]; -Sum + = mp; -            } -        } in        for(i=x;i<y;i++) -Num[i] =T[i]; to    } + } - intMain () the { *     intN; $      while(~SCANF ("%d",&N))Panax Notoginseng     { -sum=0; the Mset (n); +px1, n+1); A              for(intI=1; i<=n;i++) theprintf ("%d", Num[i]); +printf ("%i64d\n", sum); -     } $  $     return 0; -}
View Code

Summer Camp (2)-----frosh Week (UVA11858)

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.