• <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>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋    

             

            題目地址:

            http://acm.hdu.edu.cn/showproblem.php?pid=3450 

            題目描述:

            Counting Sequences

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
            Total Submission(s): 312    Accepted Submission(s): 105


            Problem Description
            For a set of sequences of integers{a1,a2,a3,...an}, we define a sequence{ai1,ai2,ai3...aik}in which 1<=i1<i2<i3<...<ik<=n, as the sub-sequence of {a1,a2,a3,...an}. It is quite obvious that a sequence with the length n has 2^n sub-sequences. And for a sub-sequence{ai1,ai2,ai3...aik},if it matches the following qualities: k >= 2, and the neighboring 2 elements have the difference not larger than d, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number of its perfect sub-sequence.
             

            Input
            Multiple test cases The first line will contain 2 integers n, d(2<=n<=100000,1<=d=<=10000000) The second line n integers, representing the suquence
             

            Output
            The number of Perfect Sub-sequences mod 9901
             

            Sample Input
            4 2 1 3 7 5
             

            Sample Output
            4
             

            題目分析 :

            ( 線段樹 || 樹狀數(shù)組 ) + DP  

            可以使用線段樹 或 樹狀數(shù)組來做,  可惜本人現(xiàn)在還不會(huì)線段樹, 下面是 樹狀數(shù)組 解題的分析 :

            因?yàn)轭}目要求的 是 k >= 2 ,  而且 相鄰元素 之差的絕對(duì)值 不大于 d,  這里糾結(jié)了很久 , 一直沒明白 "and the

            neighboring 2 elements have the difference not larger than d" 這句話的意思, 英語(yǔ)水平太差了.  最后在

            Amb 的解釋下才明白,  0rz...........  

            為了使問題簡(jiǎn)單化, 我們不凡討論 k >= 1 的情況:

             因?yàn)?輸入數(shù)據(jù)的范圍比較大,  2<=n<=100000,1<=d=<=10000000 , 所以先要將 數(shù)據(jù)排序后 用 hash[max] 做離散化處理.

            當(dāng)然, 原數(shù)組需要另開一個(gè)數(shù)組保存起來. 處理完成后,  我們可以這樣理解 :   用樹狀數(shù)組 com[pos], ( pos 為 num 在 hash 數(shù)組中的位

            置 ) . 記錄 能與 num 構(gòu)成 完美串的個(gè)數(shù).     初始狀態(tài)的 完美子串 為0, 每次加入一個(gè)數(shù), 那么這個(gè)數(shù) num 與 比他小且差不超過d的第一

            個(gè)數(shù) 而且與 比他大且差不超過d的第一個(gè)數(shù)  構(gòu)成完美串 . 更新樹狀數(shù)組. 最后求出所有的子串和 sum,  因?yàn)樗惴ㄊ且?k >= 1 來做的,而

            題目要求 k >= 2, 也就是說, 對(duì) N 個(gè)數(shù), 就多計(jì)算了 N 次, 因此 (sum - N ) % 9901 即為所求. ( 別忘了MOD....... )

             

            代碼如下 :

            /*
            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋
                      http://www.cnblog.com/MiYu
            Author By : MiYu
            Test      : 1
            Program   : 3450
            */

            #include <iostream>
            #include <algorithm>
            using namespace std;
            const int MAX = 100010;
            const int MOD = 9901;
            int nCount = 0;
            int com[MAX];
            int hash[MAX];
            int num1[MAX];
            int num2[MAX];
            int N,D;
            inline int low ( int x ) {
                   return x & ( -x );
            }
            void modify ( int x, int val ){     // 修改 
                 while ( x <= nCount ){
                        com[x] += val; x += low ( x );
                 }     
            }
            int quy ( int x ){                  // 查詢 
                int sum = 0;
                while ( x > 0 ){
                       sum += com[x]; x -= low ( x );
                }
                return sum ;
            }
            int find1 ( int val ){         // 二分 找 <= val 的第一個(gè)數(shù) 
                int beg = 1, end = nCount; int mid = ( beg + end ) >> 1; int res = mid;
                while ( beg <= end ){
                       if ( hash[mid] <= val ){
                            beg = mid + 1; res = mid;
                       }else{
                            end = mid - 1; 
                       }
                       mid = ( beg + end ) >> 1;
                }
                return res;
            }
            int find2 ( int val ){         // 二分 找 <= val 的第一個(gè)數(shù) 
                int beg = 1, end = nCount; int mid = ( beg + end ) >> 1; int res = mid;
                while ( beg <= end ){
                       if ( hash[mid] < val ){
                            beg = mid + 1;
                       }else{
                            end = mid - 1; res = mid;
                       }
                       mid = ( beg + end ) >> 1;
                }
                return res;
            }
            inline bool scan_d(int &num)      // 輸入 
            {
                    char in;bool IsN=false;
                    in=getchar();
                    if(in==EOF) return false;
                    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
                    if(in=='-'){ IsN=true;num=0;}
                    else num=in-'0';
                    while(in=getchar(),in>='0'&&in<='9'){
                            num*=10,num+=in-'0';
                    }
                    if(IsN) num=-num;
                    return true;
            }
            int main ()
            {
                while ( scan_d ( N ) && scan_d ( D ) ){
                       for ( int i = 0; i < N; ++ i ){
                            scan_d ( num1[i] );  num2[i] = num1[i];  
                       }
                       sort ( num1, num1 + N );
                       hash[1] = num1[0];  nCount = 1;
                       for ( int i = 1; i < N; ++ i ){               // 數(shù)據(jù) 離散化 
                            if ( num1[i] != num1[i-1] ) hash[++nCount] = num1[i];
                       }
                       memset ( com, 0, sizeof ( com ) );
                       for ( int i = 0; i < N; ++ i ){
                            int pos = find1 ( num2[i] );             // 找當(dāng)前元素的 hash 下標(biāo)   
                            int pre = find1 ( num2[i] + D );         // 找 <=  num2[i] + D 的第一個(gè)數(shù) 
                            int tail = find2 ( num2[i] - D );        // 找 >= num2[i] - D  的第一個(gè)數(shù) 
                            int val = quy ( pre ) - quy ( tail - 1 ) + 1;  // 區(qū)間 [ num2[i] - D, num2[i] + D ] 的元素個(gè)數(shù) + 新增的一個(gè)元素 
                            modify ( pos, val % MOD );
                       }
                       cout << ( quy ( nCount ) - N  ) % MOD << endl;  //以 一個(gè)元素 的 [ elem - D, elem + D ] 計(jì)算, 題目要求 k >= 2, 減掉 N 個(gè) 
                }
                return 0;
            }

             

             

            久久精品免费全国观看国产| 久久精品国产亚洲AV无码麻豆| 999久久久免费国产精品播放| 久久免费高清视频| 三级韩国一区久久二区综合| 国产欧美久久久精品影院| 精品久久无码中文字幕| 久久久久亚洲AV成人网人人网站| 精品无码久久久久国产动漫3d| 91久久成人免费| 亚洲va久久久噜噜噜久久男同| 国产精久久一区二区三区| 久久久久人妻一区精品色| 婷婷久久综合九色综合九七| 久久99精品久久久久久动态图| 久久亚洲日韩看片无码| 99久久精品费精品国产| 日韩精品无码久久久久久| 亚洲国产香蕉人人爽成AV片久久| 一本久久a久久精品综合夜夜| 亚洲精品无码专区久久久| 午夜精品久久影院蜜桃| 久久99精品国产麻豆不卡| 岛国搬运www久久| 99久久综合狠狠综合久久止| 久久成人国产精品免费软件| 久久午夜免费视频| 中文精品久久久久人妻| 久久综合久久性久99毛片| 精品久久久久久无码国产| 久久综合九色综合精品| 久久成人精品视频| 久久亚洲精品中文字幕三区| 99国产欧美精品久久久蜜芽| 狠狠色丁香婷综合久久| 精品精品国产自在久久高清| 久久精品亚洲一区二区三区浴池 | 亚洲国产精品婷婷久久| 97久久久久人妻精品专区| 精品久久久久久国产91| 香蕉久久一区二区不卡无毒影院|