当前位置:网站首页>Post office - post office issues (dynamic planning)
Post office - post office issues (dynamic planning)
2022-06-30 18:07:00 【The most handsome man】
Post Office
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.
Samples
Input Copy
10 5
1 2 3 6 7 9 11 22 44 50
Output
9
The main idea of the topic :
There are several villages , Then I'm going to pick out a few to build a post office , The location of the village is on a number axis , The total distance from the village to the nearest post office should be minimized , Come up with a plan , Output the shortest distance .
Ideas :
hypothesis n A village , build k individual , Then we convert it into a completed k-1 The smallest one , Then choose one of the rest to establish the minimum distance and .
Use two arrays to record :
f[i][j] : yes 1-i A village , Established j The minimum consumption of a post office
w[i][j]: yes i-j The minimum cost of setting up a post office in the village
Here, please w Array optimization needs to be understood :
If the original i and j There are even numbers between village, Suppose this time post office The construction address of is set to the position of the larger number in the median , At this time, new i after ,post office The position of the does not change , Then it is still calculated by the following formula :
w[i,j] =w[i,j-1]+a[i] - a[(j+i)/2]
· If the original i and j There are odd numbers between village( namely (i+j)%2==0), Add a new j after , You can still take the original K As the median , At this time, the median position remains unchanged , therefore
w[i,j] = w[i,j-1]+a[i] - a[(j+i)/2]
Here are the figures for understanding 
So the optimized w[i,j] = w[i,j-1]+a[i] - a[(j+i)/2]
Last last f Array transfer equation :
dp[i][j]=min(dp[i][j],dp[k][j-1]+w[k+1][i]);
AC Code :
int a[maxn];
int w[400][400],dp[400][400];
int main(){
int n = read() , m = read();
for(int i=1;i<=n;i++) cin>>a[i];// Enter location , It is itself a prefix and
for(int i=1;i<=n;i++){
for(int j=1+i;j<=n;j++){
w[i][j]=w[i][j-1]+a[j]-a[(i+j)/2];
}
}
memset(dp,0x3f3f3f3f,sizeof(dp));
for(int i=1;i<=n;i++) dp[i][i]=0,dp[i][1]=w[1][i];// It is equal when only one post office is established
for(int i=1;i<=n;i++){
for(int j=2;j<=min(m,i);j++){
// Number of post offices
//dp[i][j]=inf;
for(int k=j;k<=i-1;k++){
// Enumerate transferable locations , Of course, it is larger than the number of post offices
dp[i][j]=min(dp[i][j],dp[k][j-1]+w[k+1][i]);
}
}
}
cout<<n<<" "<<m<<" "<<dp[n][m]<<"\n";
for(int i=1;i<=n;i++) cout<<a[i]<<" ";
}
You can ask directly if you don't understand
边栏推荐
- MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
- [zero basic IOT pwn] environment construction
- 5g has been in business for three years. Where will innovation go in the future?
- ABAP-发布Restful服务
- 广电5G正式启航,黄金频段将如何应用引关注
- [bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
- Daily interview 1 question - how to prevent CDN protection from being bypassed
- Key to understanding the trend of spot Silver
- The secondary menu of the magic article system v5.4.0 supports the optimization of form display
- NFT: 开启加密艺术时代的无限可能
猜你喜欢

Rainbow Brackets 插件的快捷键

Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row

How to write a technical proposal

TFTP download kernel, NFS mount file system

What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo

Deep understanding of JVM (I) - memory structure (I)

K-line diagram must be read for quick start

Exploration and practice of "flow batch integration" in JD

ABAP publish restful service

基於SSH的網上商城設計
随机推荐
Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard
Exch: database integrity checking
Small Tools(3) 集成Knife4j3.0.3接口文档
MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
Development: how to install offline MySQL in Linux system?
基于eNSP的校园网设计的仿真模拟
同济、阿里的CVPR 2022最佳学生论文奖研究了什么?这是一作的解读
What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo
[sword finger offer] 52 The first common node of two linked lists
How to write a technical proposal
【架构】1366- 如何画出一张优秀的架构图
Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
TFTP下载kernel,nfs挂载文件系统
Deep understanding of JVM (III) - memory structure (III)
Daily interview 1 question - how to prevent CDN protection from being bypassed
基于SSM的新闻管理系统
巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...
Tubes响应性数据系统的设计与原理
Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides
ABAP publish restful service