当前位置:网站首页>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
边栏推荐
- Radio and television 5g officially set sail, attracting attention on how to apply the golden band
- New research of HKUST & MsrA: about image to image conversion, finishing is all you need
- 5G商用三年,未来创新何去何从?
- [Architecture] 1366- how to draw an excellent architecture diagram
- 应届生毕业之后先就业还是先择业?
- [sword finger offer] 53 - I. find the number I in the sorted array
- 【架构】1366- 如何画出一张优秀的架构图
- 每日面试1题-如何防止CDN防护被绕过
- The gates of Europe
- The secondary menu of the magic article system v5.4.0 supports the optimization of form display
猜你喜欢
Deep understanding of JVM (VI) -- garbage collection (III)
IEEE TBD SCI影响因子提升至4.271,位列Q1区!
Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole
A tough battle for Tencent cloud
Importing alicloud ECS locally to solve deployment problems
流批一体在京东的探索与实践
Ten thousand volumes - list sorting [01]
Deep understanding of JVM (I) - memory structure (I)
MySQL reports that the column timestamp field cannot be null
基于SSH的客户关系CRM管理系统
随机推荐
Synchronized summary
Simulation of campus network design based on ENSP
leetcode:1042. Do not plant flowers adjacent to each other [randomly fill in qualified + no contradiction will be formed behind + set.pop]
5g has been in business for three years. Where will innovation go in the future?
Six photos vous montrent pourquoi TCP serre la main trois fois?
现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
【二叉树】前序遍历构造二叉搜索树
ABAP-发布Restful服务
Deep understanding of JVM (II) - memory structure (II)
Lenovo's "dual platform" operation and maintenance solution helps to comprehensively improve the intelligent management ability of the intelligent medical industry
【剑指Offer】52. 两个链表的第一个公共节点
元宇宙带来的游戏变革会是怎样的?
Small Tools(3) 集成Knife4j3.0.3接口文档
NFT: 开启加密艺术时代的无限可能
阿里云ECS导入本地,解决部署的问题
Exch: repair the missing system mailbox
[zero basic IOT pwn] environment construction
[sword finger offer] 53 - I. find the number I in the sorted array
Solution: STM32 failed to parse data using cjson
. Net ORM framework hisql practice - Chapter 1 - integrating hisql