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

            coreBugZJ

            此 blog 已棄。

            POJ 1160 Post Office

            POJ 1160 Post Office
            Time Limit: 1000MS
            Memory Limit: 10000K
            Total Submissions: 10151
            Accepted: 5466

            Description

            There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

            Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

            You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

            Input

            Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

            Output

            The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

            Sample Input

            10 5
            1 2 3 6 7 9 11 22 44 50

            Sample Output

            9



            我的代碼 :

            簡單的 DP,未使用四邊形不等式優化 :

            #include <stdio.h>
            #include 
            <string.h>

            #define  N  309
            #define  M  39

            int n, m, x[ N ];

            int solve() {
                    
            int i, j, k, f[ N ][ M ], w[ N ][ N ], tmp;
                    
            int OO = 0x3f3f3f3f;

                    
            int t[ N ];
                    t[ 
            0 ] = 0;
                    
            for ( i = 1; i <= n; ++i ) {
                            t[ i ] 
            = t[ i - 1 ] + x[ i ];
                    }
                    
            for ( i = 1; i <= n; ++i ) {
                            w[ i ][ i ] 
            = 0;
                            
            for ( j = i + 1; j <= n; ++j ) {
                                    k 
            = ( j - i ) / 2 + i;
                                    w[ i ][ j ] 
            = t[ j ] - t[ k ] - t[ k - 1 ] + t[ i - 1 ] + x[ k ] * ( k + k - i - j );
                            }
                    }

                    memset( f, 
            0x3fsizeof(f) );
                    f[ 
            0 ][ 0 ] = 0;
                    
            for ( i = 1; i <= n; ++i ) {
                            
            for ( j = 1; j <= m; ++j ) {
                                    
            for ( k = 0; k < i; ++k ) {
                                            
            if ( f[ k ][ j - 1 ] != OO ) {
                                                    tmp 
            = f[ k ][ j - 1 ] + w[ k + 1 ][ i ];
                                                    
            if ( tmp < f[ i ][ j ] ) {
                                                            f[ i ][ j ] 
            = tmp;
                                                    }
                                            }
                                    }
                            }
                    }
                    
            return f[ n ][ m ];
            }

            int main() {
                    
            int i;
                    scanf( 
            "%d%d"&n, &m );
                    
            for ( i = 1; i <= n; ++i ) {
                            scanf( 
            "%d", x + i );
                    }
                    printf( 
            "%d\n", solve() );
                    
            return 0;
            }

            posted on 2011-03-17 18:59 coreBugZJ 閱讀(1340) 評論(0)  編輯 收藏 引用 所屬分類: ACM

            精品久久人人爽天天玩人人妻| 伊人久久大香线蕉精品不卡| 久久国产免费观看精品3| 久久丫精品国产亚洲av不卡 | 九九久久精品无码专区| 久久久久久久久久久免费精品| 无夜精品久久久久久| 97超级碰碰碰久久久久| 久久无码一区二区三区少妇| 色婷婷综合久久久中文字幕| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 性欧美丰满熟妇XXXX性久久久 | 久久久久无码精品国产| 国产福利电影一区二区三区久久久久成人精品综合 | 99热热久久这里只有精品68| 久久这里都是精品| 亚洲一本综合久久| 久久精品国产99久久久 | 久久国产精品成人片免费| 精品久久人人做人人爽综合 | 国内精品久久久久久野外| 18禁黄久久久AAA片| 94久久国产乱子伦精品免费| 99精品国产综合久久久久五月天| 久久久精品午夜免费不卡| 日产精品久久久久久久| 久久亚洲2019中文字幕| 成人亚洲欧美久久久久| 国产精品久久久久久久久| 亚洲国产精品无码成人片久久| 久久精品一区二区影院| 精品国产综合区久久久久久| 国产成人精品久久一区二区三区| av色综合久久天堂av色综合在| 热RE99久久精品国产66热| 精品视频久久久久| 久久国产免费直播| 久久露脸国产精品| 欧美午夜精品久久久久久浪潮| 久久WWW免费人成—看片| 青青青青久久精品国产h久久精品五福影院1421|