HDU2838 Cow sorting "tree array" "Reverse order Number"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2838


Main topic:

There are n cows lined up in a row. Each cow has a unique "bad temper" value. The range of bad temper is 1~100000. will now

The cows are reordered so that the cows are ranked in the order in which the bad temper increases. All cows can exchange their positions with each other. But the exchange of spleen

The gas value is x, y two cows, the time required is x+y. Now ask: What is the shortest time it takes to rearrange the cows?


Ideas:

The problem is to give you a sequence of n elements, and the sums of all the inverse numbers in the sequence. So, for the first element of value A,

In addition to knowing the number of elements in the first I element that is larger than a, I have to know the elements of the first I element that are larger than a. Building a structure tree

Array, a variable that records the number of elements smaller than a, and I-the number of elements that are smaller than a is the number of elements in the first I element that are larger than a

Number of reverse order numbers), this is to facilitate the calculation. Another variable to record the and of the elements smaller than a. n the number of and-smaller elements than a

And that's the number of the numbers that were previously larger than a.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL __    int64using namespace Std;const int maxn = 100010;struct node{int Cnt;     The number of elements in the previous I element that is larger than a is LL sum; Record the}tree[maxn];int of the elements in the previous I element that are larger than a and n;int lowbit (int i) {return I & (-i);}        void Update (int i,int v,int Cnt) {while (I <= N) {tree[i].sum + = V; Tree[i].        CNT + = CNT;    i + = Lowbit (i);    }}int querycnt (int n)//return value is a number of elements in front of a smaller than a {int ans = 0; while (n > 0) {ans + = tree[n].        Cnt;    N-= Lowbit (n); } return ans;    ll querysum (int n)//return value is the number of a in front of a small element and {LL ans = 0;        while (n > 0) {ans + = tree[n].sum;    N-= Lowbit (n); } return ans;    int main () {int A;        while (CIN >> N) {LL ans = 0;        memset (tree,0,sizeof (Tree));            for (int i = 1; I <= N; ++i) {cin >> A;            Update (a,a,1);    LL K1 = i-querycnt (a); The inverse of the first element of the K1 value of a is            if (k1! = 0) {//value is before the number of a larger number than the sum of LL K2 = Querysum (N)-querysum (a);            Ans + = k1*a + K2;    }} cout << ans << endl; } return 0;}


HDU2838 Cow sorting "tree array" "Reverse order Number"

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.