Blue Bridge Cup: Children in line

Source: Internet
Author: User

<title>Blue Bridge Cup: Children in line</title> Blue Bridge Cup: Children in line

N Children stand in a row. Now it's time to put them in the order of height from low to high, but only two children are exchanged at each location.

Every child has a degree of upset. At the beginning, all the children's unhappy degree is 0.

If a child is asked to exchange for the first time, his degree of upset increases by 1, and if he is asked to exchange for the second time, his degree of upset increases by 2 (ie, 3), and so on. When a child is asked to exchange for K, his degree of upset is increased by K.

Ask, to let all children in line from low to high, their degree of upset and minimum is how much.

If two children are of the same height, it is no matter who they stand in front of.

"Data Format"

The first line of input contains an integer n, which indicates the number of children.
The second line consists of n integers H1 H2 ... Hn the height of each child, respectively.
The output line contains an integer that indicates the child's degree of upset and the minimum value.

For example, enter:
3
3 2 1
The program should output:
9

"Sample description"
First of all, exchange height of 3 and 2 children, and then exchange height of 3 and 1 children, and then exchange height of 2 and 1 children, each child's unhappy degree is 3, the sum of 9.

"Data size and conventions"
For 10% of data,1<=n<=10;
For 30% of data,1<=n<=1000;
For 50% of data,1<=n<=10000;
For 100% of data, 1<=n<=100000,0<=hi<=1000000.

#include <iostream>using namespace STD;Const int MAXN= 1000010;struct node{int h;int T;};node a[MAXN];node aux[MAXN];int N;Long Long sum;void Merge(int L,int Mid,int R) {int I= L;int J= mid+1;int End1= Mid;int End2= R;int T= L; while(I <= end1 && J <= End2) {if(A[i].h > A[j].h)            {a[j].t + = mid-i+1;        aux[t++] = a[j++]; }Else{a[i].t + = J-1-mid;        aux[t++] = a[i++]; }    } while(J <= End2)    {aux[t++] = a[j++]; } while(I <= End1)        {a[i].t + = j-mid-1;    aux[t++] = a[i++]; } for(int k= L; K <= R;    k++) {A[k] = aux[k]; }}void Msort(int L,int R) {if(L < R) {int Mid= (R + L)/2;        Msort (L, mid);        Msort (mid+1, R);    Merge (L, Mid, R); }}int Main(void) {cin >> n; for(int I= 0; I < n;    i++) {cin >> a[i].h; } msort (0, n-1); for(int I= 0; I <= n-1; i++) {Long Long tmp= a[i].t;    Sum + = (tmp + 1) * TMP/2; } cout << sum << endl;return0;}

Blue Bridge Cup: Children in line

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.