打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
一道不错的动态规划(1160) - &豪 - C 博客

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 51 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 ;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
问路和指路英语大全
小学英语语法大全 第18讲 话题
如何利用 C# 实现 K-D Tree 结构?
Android ListView 隔行重复选中的问题
android平板上的GridView视图缓存优化
Post-80s officials compete for posts
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服