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

            久久香蕉国产线看观看99| 国产成人久久激情91| 1000部精品久久久久久久久| 亚洲伊人久久成综合人影院 | 欧美国产成人久久精品| 亚洲国产精品热久久| 久久精品国产91久久综合麻豆自制| 99久久国产精品免费一区二区| 欧美国产精品久久高清| 手机看片久久高清国产日韩| 亚洲伊人久久综合中文成人网| 香蕉久久久久久狠狠色| 久久99热这里只频精品6| 久久亚洲AV无码精品色午夜麻豆| 亚洲一级Av无码毛片久久精品| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久综合给合综合久久| 久久受www免费人成_看片中文| 久久久久久久91精品免费观看| 色播久久人人爽人人爽人人片AV| 久久福利资源国产精品999| 亚洲精品国产字幕久久不卡| 久久99精品久久久久久动态图| 2021久久国自产拍精品| 国内精品久久久久久久久电影网| 久久久久久国产精品美女| 2021国产精品午夜久久| 狠狠色婷婷久久一区二区三区| 草草久久久无码国产专区| 亚洲国产精品成人久久蜜臀 | 亚洲精品WWW久久久久久| 精品国产乱码久久久久久呢| 亚洲精品无码久久一线| 久久综合给合久久狠狠狠97色69| 一本大道加勒比久久综合| 午夜精品久久久久成人| 国内精品久久久人妻中文字幕| 久久综合伊人77777麻豆| 国产情侣久久久久aⅴ免费| 久久久久无码国产精品不卡| 国产精品久久久久jk制服|