Hdu 3743 Frosh Week (discretization + tree array)

Source: Internet
Author: User

Frosh Week

 

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission (s): 289 Accepted Submission (s): 72

Problem 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 on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. this rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. the team that finishes fastest wins. thus, in order to win, you wowould like to minimize the number of swaps required.

 

Input

The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. the following n lines each contain one integer, the student number of each student on the team. no student number will appear more than once.

 

Output

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

 

Sample Input

3 3 1 2

 

Sample Output

2

 

As with the previous question, please do not lie in English when you calculate the number of reverse orders. I want to practice discretization, So I'm more impressed by writing it again.

Code:

Cpp Code

# Include <iostream>

# Include <stdio. h>

# Include <memory. h>

# Include <algorithm>

Using namespace std;

 

Int n, a [1000005];

 

Struct node

{

Int num, id;

} Ch [1000005];

 

Bool cmp1 (node a, node B)

{

Return a. num <B. num;

}

 

Bool cmp2 (node a, node B)

{

Return a. id <B. id;

}

 

Int lowbit (int I)

{

Return I & (-I );

}

 

Void update (int I, int x)

{

While (I <= n)

{

A [I] + = x;

I + = lowbit (I );

}

}

 

Int sum (int I)

{

Int sum = 0;

While (I> 0)

{

Sum + = a [I];

I-= lowbit (I );

}

Return sum;

}

 

Int main ()

{

Int I;

Long ans; // pay attention to long !!!

While (scanf ("% d", & n )! = EOF)

{

Ans = 0;

Memset (a, 0, sizeof ());

For (I = 1; I <= n; I ++)

{

Scanf ("% d", & ch [I]. num );

Ch [I]. id = I;

}

/// Discretization

Sort (ch + 1, ch + n + 1, cmp1); // sort by value

Ch [1]. num = 1; // directly discretization his value

For (I = 2; I <= n; I ++)

{

If (ch [I]. num! = Ch[ I-1]. num)

Ch [I]. num = I;

Else ch [I]. num = ch [I-1]. num;

}

Sort (ch + 1, ch + n + 1, cmp2); // sort by id again

/// Complete discretization

For (I = 1; I <= n; I ++)

{

Update (ch [I]. num, 1 );

Ans + = (sum (n)-sum (ch [I]. num ));

}

Printf ("% I64d \ n", ans );

}

 

Return 0;

}

Related Article

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.