<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      HDU Cow Sorting (樹狀數組)

      題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2838

                                      Cow Sorting

       

      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). 

       

       1 #include<stdio.h>
       2 #include<string.h>
       3 #define ll __int64
       4 ll c[100000+5],v[100000+5];
       5 int n;
       6 int Lowbit(int k)
       7 {
       8     return (k&-k);
       9 }
      10 void update(int pos,int num,int val)
      11 {
      12     while(pos<=n)
      13     {
      14         c[pos]+=num;
      15         v[pos]+=val;
      16         pos+=Lowbit(pos);
      17     }
      18 }
      19 ll sum_count(int pos)
      20 {
      21     ll  s=0;
      22 
      23     while(pos>0)
      24     {
      25         s+=c[pos];
      26         pos-=Lowbit(pos);
      27     }
      28     return s;
      29 }
      30 ll sum(int pos)
      31 {
      32     ll s=0;
      33 
      34     while(pos>0)
      35     {
      36         s+=v[pos];
      37         pos-=Lowbit(pos);
      38     }
      39     return s;
      40 }
      41 int main()
      42 {
      43     int i,x;
      44     while(scanf("%d",&n)!=-1)
      45     {
      46         memset(c,0,sizeof(c));
      47         memset(v,0,sizeof(v));
      48         ll ans=0,k2,k1;
      49         for(i=1;i<=n;i++)
      50         {
      51             scanf("%d",&x);
      52             update(x,1,x);
      53             k1=i-sum_count(x);///到此為止  比x大的個數;
      54 /*sum_count[x] 為輸入i個數的時候 x之前有sum_count[x]個比x小的數 用i相減則為大于x的個數*/
      55             if(k1!=0)
      56             {
      57             k2=sum(n)-sum(x);///到此為止  比x大的數的和;
      58             ans+=x*k1+k2;///到此為止 比x大的數與x交換之后的和;
      59             }
      60         }
      61         printf("%I64d\n",ans);
      62     }
      63     return 0;
      64 }
      View Code

       

      /*坑爹的題    必須用 __int64 不能用long  long  不能用int  ╮(╯▽╰)*/

       

       

      posted @ 2014-08-15 20:05  四十四次日落  Views(572)  Comments(0)    收藏  舉報
      主站蜘蛛池模板: 无码内射中文字幕岛国片| 国产成人不卡一区二区| 色综合久久久久综合体桃花网| 亚洲成av人片无码天堂下载| 国产精品高清一区二区三区| 亚洲色欲色欲www在线看| 色综合久久一区二区三区| 女人腿张开让男人桶爽| 无码精品一区二区免费AV| 亚洲色婷婷久久精品av蜜桃久久| 老熟妇国产一区二区三区| 老司机午夜精品视频资源| 久久夜色精品亚洲国产av| 亚洲欧美牲交| 日韩理伦片一区二区三区| 日本黄页网站免费观看| 人妻中文字幕精品系列| 国产国产久热这里只有精品| 国产精品亚洲аv无码播放| 无码中文字幕av免费放| 蜜臀av无码一区二区三区| 色综合色综合久久综合频道88 | 久久99国产精品尤物| 国产玖玖视频| 日韩中文字幕亚洲精品| 天堂在线中文| 国产成人高清亚洲一区二区| 亚洲乱码中文字幕小综合| 无码日韩做暖暖大全免费不卡| 福利网午夜视频一区二区| 国偷自产一区二区免费视频| 亚洲激情av一区二区三区| 中文字幕结果国产精品| 欧美xxxxx高潮喷水| 伦理片午夜视频在线观看| 亚洲大尺度无码专区尤物| 英德市| 亚洲av综合色一区二区| 国产精品美女AV免费观看| 亚洲美免无码中文字幕在线| 色伊人久久综合中文字幕|