• <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,未使用四邊形不等式優(yōu)化 :

            #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 閱讀(1347) 評論(0)  編輯 收藏 引用 所屬分類: ACM

            久久最新免费视频| 蜜臀av性久久久久蜜臀aⅴ麻豆| 性做久久久久久久| 久久男人Av资源网站无码软件 | 丰满少妇人妻久久久久久4| 青青青青久久精品国产| 热久久国产欧美一区二区精品| 一级做a爰片久久毛片看看 | 久久天天躁狠狠躁夜夜网站 | 奇米综合四色77777久久| 精品国产福利久久久| 一本色道久久88综合日韩精品| 久久亚洲私人国产精品vA| 亚洲国产精品无码久久久久久曰| 国产激情久久久久久熟女老人| 国内精品久久久久久久coent| 婷婷伊人久久大香线蕉AV| 久久人人爽人爽人人爽av| 69久久精品无码一区二区| 久久久久久综合网天天| 国産精品久久久久久久| 99999久久久久久亚洲| 精品久久久无码人妻中文字幕| 亚洲国产精品久久66| 久久99久久99精品免视看动漫| 亚洲人成网站999久久久综合 | 久久精品国产亚洲AV蜜臀色欲| 91久久精品国产免费直播| 色狠狠久久AV五月综合| 国产A三级久久精品| 久久天天躁狠狠躁夜夜躁2014| 久久性精品| 一97日本道伊人久久综合影院| 久久免费99精品国产自在现线| 日韩精品久久久久久| 久久免费小视频| 一本伊大人香蕉久久网手机| 国产一级持黄大片99久久| 情人伊人久久综合亚洲| 久久午夜电影网| 免费观看成人久久网免费观看|