HDU 2838 Cow Sorting tree-like array

Source: Internet
Author: User

HDU 2838


"Cow sorting" This question originally elated want to use Java to do again, the result after the infinite super memory, really ah, do the problem Java this thing still easy not to move. There is also the feeling that the numbers must be discretized, the results of background data do not need to be discretized.


Test instructions: Give an n for N Bulls, and then give n the number I think is n (including N). Though I didn't say it in dignity. Then can only adjacent two digital exchange position, will let the cow produce rage value, the value is the sum of the number of the two cattle that move to each other, ask the cow according to the small to large order after the smallest cow generated rage.


Idea: actually want the minimum rage value you just don't have to work hard that no matter how the movement is the minimum value. Do not work hard to put a small number of cows in the large number of cattle on the line. Then the rage value is (the number of cows that are older than the current cow number) * (number of current cows) + (the number of cows before the current cow is larger than the current number of cattle), note the long long.


Code

#include  <stdio.h> #include  <string.h> #include  <iostream> #include  < math.h> #include  <algorithm>using namespace std; #define  ll long  Longstruct node{    int qx;    long long sum;} C[100001];int n;int lowbit (int k) {    return k &  (-K);} Void add (Int x, int val, int vall) {    while  (x  <= n)     {        c[x].qx += val ;        c[x].sum += vall;         x += lowbit (x);     }}ll queryqx (int x) {     ll sum1 = 0;    while  (x > 0)      {        sum1 += c[x].qx;        x -=  lowbit (x);     }    return sum1;} Ll querysum (int x) {    ll sum2 = 0;    while   (x > 0)     {        sum2 + = c[x].sum;        x -= lowbit (x);     }    return sum2;} Int main () {    while  (scanf ("%d",  &n)  == 1)      {        int cow;         ll tqx, ans = 0, tsum;        memset (C, &NBSP;0,&NBSP;SIZEOF&NBSP;C);        for  (int i = 0; i < n; i++)         {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &cow);             add (Cow, 1, cow);      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;TQX&NBSP;=&NBSP;I+1-QUERYQX (Cow);             if  (tqx > 0)              {                 tsum = querysum (n)  - querysum (cow);                 ans += tqx*cow + tsum;             }         }        printf ("%lld\n",  ans);     }    return  0;}


This article is from the "Flaergwe" blog, make sure to keep this source http://flaergwe.blog.51cto.com/10217882/1653609

HDU 2838 Cow Sorting tree-like array

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.