Previous questions children queue time limit: 1.0s memory limit: 256.0MBProblem 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. Count the number of the left of each number that is larger than him, and the number on the right that is smaller than his.
Code:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define LL Long longconst int M = 1e6+5;using namespace std; LL C[m], sum[m], a[m], b[m], c2[m]; ll bit (ll x) {return x & x;} void Add (LL x) {while (X < M) {c[x]++;x + = bit (x);}} ll Getsum (ll x) {ll sum = 0;while (x) {sum + = c[x];x-= bit (x);} return sum;} int main () {LL n, i;cin >> n;for (i = 1; i < M; + + i) sum[i] = sum[i-1]+i;for (i = 0; i < n; + + i) { //current point and left point Reverse logarithm of the formation of CIN >> A[i];
Blue Bridge cup children queue "tree array" + "reverse order Number"