• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj 2299 Ultra-QuickSort 樹狀數(shù)組

            求逆序?qū)?shù),樹狀數(shù)組

            數(shù)據(jù)范圍較大,要離散化。

            給每一個(gè)數(shù)據(jù)一個(gè)id, 第i個(gè)數(shù)據(jù)的id為i。 然后從小到大排序,對(duì)于每個(gè)id做 ans += read(n) - read(array[i].id),read(n) - read(array[i].id)表示原來在當(dāng)前數(shù)的后面(其id大于當(dāng)前數(shù)的id),
            現(xiàn)在在當(dāng)前數(shù)前面的數(shù)個(gè)數(shù),也就是逆序?qū)?shù)。


            #include<iostream>
            #include
            <cstring>
            #include
            <cstdio>
            #include
            <algorithm>
            using namespace std;
            const int MAXVAL = 500005;

            int tree[MAXVAL] ;
            struct Type
            {
                
            int num, id;
            };

            int n;
            Type array[MAXVAL];

            void update(int idx, int inc)  //更新idx的頻率
            {
                
            while(idx <= n)
                {
                    tree[idx] 
            += inc;
                    idx 
            += (idx & - idx);
                }
            }

            int read(int idx)   //讀取1--idx的頻率和
            {
                
            int sum = 0;
                
            while(idx > 0)
                {
                    sum 
            += tree[idx];
                    idx 
            -= (idx & - idx);
                }
                
            return sum;
            }

            int readSingle(int idx) // 讀取某個(gè)位置的頻率, O(lg MAXVAL)
            {
                 
            int sum = tree[idx];
                 
            if(idx > 0)
                 {
                     
            int z = idx - ( idx & - idx);  
                     
                     idx 
            --;

                     
            while( idx != z)
                     {
                          sum 
            -= tree[idx];

                          idx 
            -= (idx & - idx);
                     }
                 }

                 
            return sum;
            }


            bool cmp(const  Type &a, const Type &b)
            {
                
            return a.num < b.num;
            }
            int main()
            {
                
            while (scanf("%d",&n)  && n != 0)    
                {
                    memset(array, 
            0sizeof (array));
                    memset(tree, 
            0sizeof tree);

                    
            // read the data
                    for(int i = 1; i <= n; i ++)
                    {
                        scanf(
            "%d",&array[i].num);
                        array[i].id 
            = i;
                    }
                
                    sort(array 
            + 1, array + 1 + n, cmp);

                    
            long long ans = 0;
                    
            for(int i = 1; i <= n; i ++)
                    {
                        
            //printf( "cal   %d \n",read(n) - read(array[i].id));
                        ans += read(n) - read(array[i].id);
                        update(  array[i].id, 
            1);
                    }
                        
                    cout 
            << ans << endl;
                }


                
            return 0;
            }

            posted on 2011-03-16 20:49 田兵 閱讀(419) 評(píng)論(2)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)

            評(píng)論

            # re: poj 2299 Ultra-QuickSort 樹狀數(shù)組 2011-04-12 09:25 銀志圓

            排序用sort不太妥當(dāng)吧 sort是不穩(wěn)定排序 如果給定的序列存在多個(gè)相同的元素會(huì)出現(xiàn)錯(cuò)誤吧 盡管這個(gè)程序oj上能ac。
            大概oj上給定的數(shù)據(jù)是互不相同的吧   回復(fù)  更多評(píng)論   

            # re: poj 2299 Ultra-QuickSort 樹狀數(shù)組 2011-04-19 21:27 田兵

            有個(gè)id, id小的在前面  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            亚洲va中文字幕无码久久不卡| 国产精品久久久久天天影视| A狠狠久久蜜臀婷色中文网| 久久中文字幕人妻熟av女| 国产毛片久久久久久国产毛片 | 国产精品丝袜久久久久久不卡| 久久久久久久97| 成人久久综合网| 97精品国产91久久久久久| 99999久久久久久亚洲| 国产精品久久自在自线观看| 97精品国产91久久久久久| 久久91精品国产91久久麻豆| 亚洲狠狠综合久久| 久久久久亚洲精品中文字幕| 亚洲国产成人久久综合野外 | 久久久久无码精品| 亚洲午夜福利精品久久| 久久婷婷五月综合国产尤物app| 18禁黄久久久AAA片| 精品熟女少妇a∨免费久久| 久久99热国产这有精品| 久久中文字幕视频、最近更新 | 天天躁日日躁狠狠久久| 99久久精品国产免看国产一区| 国产成人99久久亚洲综合精品| 欧美国产成人久久精品| 无码超乳爆乳中文字幕久久| 久久免费国产精品一区二区| 亚洲国产精品无码久久久久久曰| 久久水蜜桃亚洲av无码精品麻豆| 久久er热视频在这里精品| 亚洲国产高清精品线久久| 久久香蕉综合色一综合色88| 中文字幕无码av激情不卡久久| 99久久这里只有精品| 久久综合色区| 成人国内精品久久久久影院| 国产精品乱码久久久久久软件| 99久久99久久| 午夜精品久久久久久久|