Blue Bridge Cup small pot friends lined up

Source: Internet
Author: User

Problem description N A child stands 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. Input 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 format outputs a line that contains an integer that indicates the child's level of upset and the minimum value. Sample Input 3
3 2 1 Sample Output 9 example shows that first 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 <algorithm> #include <iostream>using namespace Std;long long n=0;int  ang[100000]={0};   Swap times Long long heigt[100000]={0};//height array long long sum=0;int main () {cin>>n; for (int i=0;i<n;i++) {cin> >heigt[i];} Initializes the height array for (int. i=0;i<n;i++) for  (int j=i;j<n;j++) {  if (Heigt[i]>heigt[j])   {   ang[i]++;   ang[j]++;   } }//looking for reverse order to//Mark good swap times for        (int i=0;i<n;i++)         {       sum=sum+ (ang[i]+1) *ang[i]/2;//accumulate            }              cout< <sum;    return 0;}

  

Blue Bridge Cup small pot friends lined up

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.