zoukankan      html  css  js  c++  java
  • hdu 2838 Cow Sorting (树状数组)

    Cow Sorting

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2185    Accepted Submission(s): 683


    Problem Description
    Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y.

    Please help Sherlock calculate the minimal time required to reorder the cows.
     
    Input
    Line 1: A single integer: N
    Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
     
    Output
    Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
     
    Sample Input
    3
    2 3 1
     
    Sample Output
    7
     
    Hint
    Input Details Three cows are standing in line with respective grumpiness levels 2, 3, and 1. Output Details 2 3 1 : Initial order. 2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4). 1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).
     
    Source
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  3450 2227 3030 2642 2836 
     

    题意:

        求逆序数两两的总和: 如 3 2 1 :sum=(3+2)+(3+1)+(2+1)=12;   1 2 3: sum=0;

    树状数组:

        其实这题并不难,抓住一个点和熟悉树状数组大概就可以做出来了,那个点就是如何求得和。

        这里才用的方法是参考别人的 ,自己想了一段时间没想出来。

        对于新插入的一个元素,运用树状数组,可以求得比它小的元素的个数,比它小的元素的和,在它之前的元素的总和。

        而对于每一个新元素,其sum[m]=m*(比它大的元素个数)+(前i个元素的和)-(比它小的元素的和)。

        然后累加得解。

    实现:

     1 //46MS    2584K    955 B    C++
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define ll __int64
     5 #define N 100005
     6 ll cnt[N],ssum[N],tsum[N];
     7 inline ll lowbit(ll k)
     8 {
     9     return k&(-k);
    10 }
    11 void update(ll c[],ll k,ll detal)
    12 {
    13     for(;k<N;k+=lowbit(k))
    14         c[k]+=detal;
    15 }
    16 ll getsum(ll c[],ll k)
    17 {
    18     ll s=0;
    19     for(;k>0;k-=lowbit(k))
    20         s+=c[k];
    21     return s;
    22 }
    23 int main(void)
    24 {
    25     ll n,m;
    26     while(scanf("%I64d",&n)!=EOF)
    27     {
    28         memset(cnt,0,sizeof(cnt));
    29         memset(ssum,0,sizeof(ssum));
    30         memset(tsum,0,sizeof(tsum));
    31         ll s=0,temp=0;
    32         for(ll i=1;i<=n;i++){
    33             scanf("%I64d",&m);
    34             update(cnt,m,1); 
    35             update(ssum,m,m);
    36             update(tsum,i,m);
    37             temp=getsum(cnt,m-1);
    38             s+=m*(i-temp-1);
    39             s+=getsum(tsum,i-1);
    40             s-=getsum(ssum,m-1); 
    41             //printf("**%I64d
    ",s);
    42         } 
    43         printf("%I64d
    ",s);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    博客新域名www.tecchen.tech
    [转载]Redis 持久化之RDB和AOF
    基于zookeeper集群的云平台-配置中心的功能设计
    22部漫威电影大合集和观影顺序
    类的嵌套定义、使用以及用头文件实现多个文件编程(匠心之作C++ p105 22 )
    Python的print格式
    Tensorflow2.0+Anaconda + Windows10+cuda10.0+python(转自豌豆代理)
    C语言中用scanf连续输入两个字符类型的问题(转)
    算法和流程图(及N-S流程图)
    git的安装及针对码云的操作(二)
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3690889.html
Copyright © 2011-2022 走看看