• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2006年8月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 217875
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

            Post Office
            Time Limit:1000MS? Memory Limit:10000K
            Total Submit:1047 Accepted:456

            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

            Source
            IOI 2000

            #include? < iostream >
            using ? namespace ?std;

            /*
            p表示i到j的建一個郵局的最小值
            q表示前i個地點建j個郵局的最小值?
            dp方程

            ?????????????????p[1][i]???????????????(j?==?1)
            ?q[i][j]?=?{????????????????????????????????????????????????????}
            ????????????????q[k][j-1]?+?p[k+1][i]??(j?>?1)?(k從j-1到i-1)

            */
            ?

            int ?p[ 301 ][ 301 ];
            int ?q[ 301 ][ 31 ];
            int ?a[ 301 ];

            int ?main()
            {
            ????
            int ?V,?P;
            ????
            int ?i,?j,?k,?l;
            ????
            int ?t[ 301 ];
            ????
            int ?tmp;
            ????scanf(
            " %d%d " ,? & V,? & P);
            ????
            ????
            for ?(i = 1 ;?i <= V;?i ++ )
            ????????scanf(
            " %d " ,? & a[i]);
            ????
            ????
            for ?(i = 1 ;?i <= V;?i ++ )
            ????????
            for ?(j = i;?j <= V;?j ++ )
            ????????
            {
            ????????????
            if ?(i? == ?j)
            ????????????????p[i][j]?
            = ? 0 ;
            ????????????
            else
            ????????????
            {
            ????????????????l?
            = ?(i? + ?j)? / ? 2 ;
            ????????????????p[i][j]?
            = ? 0 ;
            ????????????????
            for ?(k = i;?k <= l;?k ++ )
            ????????????????????p[i][j]?
            += ?a[l]? - ?a[k];
            ????????????????
            for ?(k = l + 1 ;?k <= j;?k ++ )
            ????????????????????p[i][j]?
            += ?a[k]? - ?a[l];
            ???????????????
            ????????????}

            ????????}

            ????????
            ????memset(q,?
            0 ,? sizeof (q));
            ????
            for ?(i = 1 ;?i <= V;?i ++ )
            ????????
            for ?(j = 1 ;?j <= P;?j ++ )
            ????????
            {
            ????????????
            if ?(j? == ? 1 )
            ????????????????q[i][j]?
            = ?p[ 1 ][i];
            ????????????
            else
            ????????????
            {
            ????????????????
            if ?(i? >= ?j)
            ????????????????
            {
            ????????????????????q[i][j]?
            = ?q[j - 1 ][j - 1 ]? + ?p[j][i];
            ????????????????????
            for ?(k = j;?k < i;?k ++ )
            ????????????????????
            {
            ????????????????????????
            if ?(q[i][j]? > ?q[k][j - 1 ]? + ?p[k + 1 ][i])
            ????????????????????????????q[i][j]?
            = ?q[k][j - 1 ]? + ?p[k + 1 ][i];
            ????????????????????}

            ????????????????}

            ????????????}

            ????????}

            ????????
            ????cout?
            << ?q[V][P]? << ?endl;
            ????system(
            " pause " );
            ????
            return ? 0 ;
            }

            posted on 2006-09-01 22:33 閱讀(451) 評論(0)  編輯 收藏 引用 所屬分類: ACM題目
            97超级碰碰碰久久久久| 亚洲国产综合久久天堂 | 中文成人无码精品久久久不卡 | 久久婷婷五月综合97色直播| 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久无码不卡| 国产激情久久久久久熟女老人| 国产人久久人人人人爽| 国产精品永久久久久久久久久 | 久久国产精品成人影院| 久久国产精品免费一区二区三区| 久久久久久噜噜精品免费直播| 亚洲AV日韩AV天堂久久| 国内精品久久久久影院网站| AV无码久久久久不卡蜜桃| 精品久久久久久久久久中文字幕 | 精品久久人妻av中文字幕| 久久国产成人午夜aⅴ影院| 久久久一本精品99久久精品88| 久久精品国产99久久香蕉| 久久久久久毛片免费播放| 合区精品久久久中文字幕一区| 久久99精品综合国产首页| 少妇久久久久久久久久| 久久婷婷是五月综合色狠狠| 久久精品国产一区二区三区| 狠狠干狠狠久久| 狠狠狠色丁香婷婷综合久久五月| 亚洲中文精品久久久久久不卡| 久久亚洲天堂| 久久免费99精品国产自在现线 | 久久精品国产清自在天天线| 久久99久久99小草精品免视看| 99久久国产热无码精品免费| 亚洲精品乱码久久久久久按摩| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 伊人情人综合成人久久网小说| 久久这里有精品视频| 色天使久久综合网天天| 四虎国产精品成人免费久久| 日本久久中文字幕|