• <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>
            隨筆 - 97, 文章 - 22, 評論 - 81, 引用 - 0
            數據加載中……

            PKU 1990 MooFest

            題目鏈接:http://poj.org/problem?id=1990

            /*
            題意:
                約翰農夫有N(N <= 20000)頭牛,每頭牛有一個權值Vi,他將它們排成一排,
            牛i和牛j的閾值是  兩者的距離差*max(Vi, Vj),現在給出每頭牛的權值和它的
            位置,求所有兩頭牛之間的閾值之和。

            題解:
                樹狀數組

            思路:
                我們準備兩個樹狀數組,以每頭牛的位置作為樹狀數組的下標,其中一個用
            來表示當前位置的牛的位置的值,另一個則記錄當前位置牛的個數,然后對所有
            牛以Vi為關鍵字進行遞增排序。
                接下來對每頭牛進行一次線掃,首先統計比當前這頭牛的位置小的和大的牛
            的數目和位置和,然后做差求出以當前牛的權值為最大值的閾值總和。之后將這
            頭牛的數量和位置插入到樹狀數組中進行更新。
            */


            #include 
            <iostream>
            #include 
            <algorithm>

            using namespace std;

            #define maxn 20010
            #define ll __int64

            struct point {
                
            int V;
                
            int pos;
            }
            pt[maxn];

            ll c[
            2][maxn];
            int n, Max;

            ll ABS(ll v) 
            {
                
            return v < 0 ? -v : v;
            }


            int cmp(point a, point b) {
                
            return a.V < b.V;
            }


            int lowbit(int x) {
                
            return x & (-x);
            }


            void add(int idx, int pos, int v) {
                
            while(pos <= Max) {
                    c[idx][pos] 
            += v;
                    pos 
            += lowbit(pos);
                }

            }


            ll sum(
            int idx, int pos) {
                ll s 
            = 0;
                
            while(pos > 0{
                    s 
            += c[idx][pos];
                    pos 
            -= lowbit(pos);
                }

                
            return s;
            }



            int main() {
                
            int i;
                
            while(scanf("%d"&n) != EOF) {
                    Max 
            = 0;
                    
            for(i = 0; i < n; i++{
                        scanf(
            "%d %d"&pt[i].V, &pt[i].pos);
                        
            if(pt[i].pos > Max)
                            Max 
            = pt[i].pos;
                    }


                    
            for(i = 1; i <= Max; i++)
                        c[
            0][i] = c[1][i] = 0;
                    sort(pt, pt 
            + n, cmp);

                    ll ans 
            = 0;
                    
            for(i = 0; i < n; i++{
                        ans 
            += ABS((sum(0, Max) - sum(0, pt[i].pos)
                             
            - (sum(1, Max) - sum(1, pt[i].pos)) * pt[i].pos)) * pt[i].V;

                        ans 
            += ABS((sum(0, pt[i].pos)
                             
            - sum(1, pt[i].pos) * pt[i].pos)) * pt[i].V;

                        add(
            0, pt[i].pos, pt[i].pos);
                        add(
            1, pt[i].pos, 1);
                    }

                    printf(
            "%I64d\n", ans);
                }

                
            return 0;
            }

            posted on 2011-04-09 22:06 英雄哪里出來 閱讀(1318) 評論(0)  編輯 收藏 引用 所屬分類: 樹狀數組

            国产三级精品久久| 国产精品久久网| 久久AV无码精品人妻糸列| 无码人妻精品一区二区三区久久| 久久www免费人成看片| 国产精品99久久精品| 91久久精品电影| 午夜天堂av天堂久久久| 国产成人精品久久一区二区三区| 久久综合九色综合欧美狠狠| 怡红院日本一道日本久久| 亚洲婷婷国产精品电影人久久| 久久婷婷五月综合色奶水99啪| 久久黄视频| 久久精品国产亚洲AV麻豆网站| 久久性精品| 国产成人久久久精品二区三区| 一本一道久久综合狠狠老| 久久男人AV资源网站| 精品午夜久久福利大片| 亚洲欧美久久久久9999| 国产成人久久精品二区三区| 亚洲熟妇无码另类久久久 | 一本色道久久综合狠狠躁| 久久AⅤ人妻少妇嫩草影院| 7777久久亚洲中文字幕| 亚洲午夜久久久久妓女影院| 久久婷婷国产剧情内射白浆| 久久久久人妻一区精品| 嫩草影院久久国产精品| 91久久精一区二区三区大全| 欧洲精品久久久av无码电影| 99久久精品免费看国产一区二区三区| 亚洲а∨天堂久久精品| 中文精品久久久久人妻| 亚洲欧美久久久久9999| 性做久久久久久久久| 国产免费久久精品99re丫y| 一本一道久久a久久精品综合| 亚洲国产精品成人久久蜜臀| 日批日出水久久亚洲精品tv|