• <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
            數據加載中……

            HDU 3450 Counting Sequences

            題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3450
            /*
            題意:
                給定N(N <= 100000)個數字ai和一個H,要求求出特殊序列的數量
            ,所謂特殊序列,就是相鄰兩個數字的絕對值小于等于H并且序列長度
            大于等于2。

            解法:
            樹狀數組 + 動態規劃

            思路:
                 首先我們利用dp[i]表示到第i個位置能夠找到的相鄰數字之差小
            于等于H的長度大于等于1的序列的總和,那么有狀態轉移方程
            dp[i] = sum{ dp[j], j<i, abs(a[j]-a[i]) <= H },這個做法的時間
            復雜度是O(n^2),但是n很大,所以不能采用,但是我們觀察到這個轉移
            方程是以求和的形式出現,并且有一個限制條件就是abs(a[j]-a[i])<=H
            ,我們可以把它簡寫成a[i]-H <= a[j] <= a[i]+H,那么如果我們把數
            字映射到下標,并且通過二分找到a[j]的范圍,就可以輕松的通過樹狀
            數組的成段求和來統計了。
                具體做法是:由于數字較大,我們可以先將所有數字離散化,這樣
            每個數字就有一個 <= n 的標號,然后這個標號就可以對應樹狀數組的
            下標了,每次從左往右在樹狀數組中統計[a[i]-H, a[i]+H]的解的數量
            (注意,這里需要找到離散后對應的數字),然后將當前數字(離散后
            的數字)插入到樹狀數組中,值即為先前找到的節的數量,循環結束,
            累加和就是序列大于等于1的解的數量,然后再減去n就是最后的答案了
            ,這里注意是取模,并且保證答案不能為負數。
            */


            #include 
            <iostream>
            #include 
            <cstdio>
            #include 
            <cstring>
            #include 
            <algorithm>
            using namespace std;

            #define maxn 100010
            #define mod 9901

            int n;
            int val[maxn], t[maxn], c[maxn];
            int tot;

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


            void add(int pos, int v) {
                
            while(pos <= tot) {
                    c[pos] 
            += v; 
                    
            if(c[pos] >= mod ) 
                        c[pos] 
            %= mod;
                    pos 
            += lowbit(pos);
                }

            }


            int sum(int pos) {
                
            int s = 0;
                
            while(pos >= 1{
                    s 
            += c[pos];
                    
            if(s >= mod ) 
                        s 
            %= mod;
                    pos 
            -= lowbit(pos);
                }

                
            return s;
            }


            int Bin(int v) {
                
            int l = 1;
                
            int r = tot;
                
            while(l <= r) {
                    
            int m = (l + r) >> 1;
                    
            if(v == t[m])
                        
            return m;
                    
            if(v > t[m])
                        l 
            = m + 1;
                    
            else
                        r 
            = m - 1;
                }

            }


            // 找到最小的大于等于它的數
            int BThan(int v) {
                
            int l = 1;
                
            int r = tot;
                
            int ans = 1;
                
            while(l <= r) {
                    
            int m = (l + r) >> 1;
                    
            if(t[m] >= v) {
                        r 
            = m - 1;
                        ans 
            = m;
                    }
            else
                        l 
            = m + 1;
                }

                
            return ans;
            }


            // 找到最大的小于等于它的數
            int LThan(int v) {
                
            int l = 1;
                
            int r = tot;
                
            int ans = tot;
                
            while(l <= r) {
                    
            int m = (l + r) >> 1;
                    
            if(t[m] <= v) {
                        l 
            = m + 1;
                        ans 
            = m;
                    }
            else
                        r 
            = m - 1;
                }

                
            return ans;
            }


            int H;

            int main() {
                
            int i;
                
            while(scanf("%d %d"&n, &H) != EOF) {
                    
            for(i = 0; i < n; i++{
                        scanf(
            "%d"&val[i]);
                        t[i
            +1= val[i];
                    }

                    tot 
            = 0;
                    sort(t
            +1, t+1+n);
                    
            for(i = 1; i <= n; i++{
                        
            if(i==1 || t[i] != t[i-1]) {
                            t[
            ++tot] = t[i];
                            c[tot] 
            = 0;
                        }

                    }


                    
            int ans = 0;
                    
            for(i = 0; i < n; i++{
                        
            int nidx = Bin(val[i]);
                        
            int l = BThan(val[i] - H);
                        
            int r = LThan(val[i] + H);
                        
            int preVal = (sum(r) - sum(l-1)) % mod;
                        
            if(preVal < 0)
                            preVal 
            += mod;
                        ans 
            += preVal + 1if(ans >= mod) ans %= mod;
                        add(nidx, preVal 
            + 1);
                    }

                    printf(
            "%d\n", ((ans - n) % mod + mod) % mod);
                }

                
            return 0;
            }

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

            欧美一区二区三区久久综合| 伊人久久精品线影院| 久久人人爽人人精品视频| 久久成人国产精品| 色诱久久久久综合网ywww| 亚洲精品乱码久久久久久久久久久久| 久久乐国产精品亚洲综合| 国内精品伊人久久久久网站| 秋霞久久国产精品电影院| 狠狠久久亚洲欧美专区 | 久久天天躁夜夜躁狠狠| 2021国产精品久久精品| 久久笫一福利免费导航 | 国产精久久一区二区三区| 久久精品免费网站网| 少妇久久久久久被弄到高潮 | 日本欧美久久久久免费播放网| 伊人久久久AV老熟妇色| 国产精品久久永久免费| 久久久国产精品| 日韩精品无码久久久久久| 久久久久久午夜成人影院| 亚洲一区中文字幕久久| 久久精品国产久精国产一老狼| 久久精品午夜一区二区福利| 国内精品人妻无码久久久影院| 久久99热这里只有精品国产 | 久久精品99久久香蕉国产色戒| 亚洲综合精品香蕉久久网97| 婷婷久久五月天| 久久免费高清视频| 久久99精品久久久大学生| 久久―日本道色综合久久| 性欧美大战久久久久久久| 久久精品国产清高在天天线| 国产精品成人无码久久久久久| 怡红院日本一道日本久久 | 久久久久久久久久免免费精品| 久久人人爽人人爽人人av东京热| 亚洲狠狠久久综合一区77777| 久久综合国产乱子伦精品免费|