当前位置:网站首页>April 30, 2021: there are residential areas on a straight line, and the post office can only be built on residential areas. Given an ordered positive array arr
April 30, 2021: there are residential areas on a straight line, and the post office can only be built on residential areas. Given an ordered positive array arr
2022-06-24 15:55:00 【Fuda scaffold constructor's daily question】
2021-04-30: There are settlements on a straight line , Post offices can only be built on residential areas . Given an ordered array of positive numbers arr, Each value represents One dimensional coordinates of the settlement , Give me a positive number num, Indicates the number of post offices . choice num Settlements were established num individual Post Office , Minimize the total distance between all residential areas and the nearest post office , Return the shortest total distance .【 give an example 】arr=1,2,3,4,5,1000,num=2. The first post office was established in 3 Location , The second post office is set up in 1000 Location . that 1 The distance from the location to the post office by 2, 2 The distance from the location to the post office is 1,3 The distance from the location to the post office is 0,4 The distance from the location to the post office is 1, 5 The distance from the location to the post office Leave for 2,1000 The distance from the location to the post office is 0. The total distance under this scheme is 6, The total distance of any other scheme will not Shorter than the total distance of the scheme , So back 6.
Fuda answer 2021-04-30:
Dynamic programming .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"math"
)
func main() {
arr := []int{1, 2, 3, 4, 5, 1000}
num := 2
ret := min1(arr, num)
fmt.Println(ret)
//ret = min2(arr, num)
//fmt.Println(ret)
}
func min1(arr []int, num int) int {
if num < 1 || len(arr) < num {
return 0
}
N := len(arr)
w := make([][]int, N+1)
for i := 0; i < N+1; i++ {
w[i] = make([]int, N+1)
}
for L := 0; L < N; L++ {
for R := L + 1; R < N; R++ {
w[L][R] = w[L][R-1] + arr[R] - arr[(L+R)>>1]
}
}
dp := make([][]int, N)
for i := 0; i < N; i++ {
dp[i] = make([]int, num+1)
}
for i := 0; i < N; i++ {
dp[i][1] = w[0][i]
}
for i := 1; i < N; i++ {
for j := 2; j <= getMin(i, num); j++ {
ans := math.MaxInt32
for k := 0; k <= i; k++ {
ans = getMin(ans, dp[k][j-1]+w[k+1][i])
}
dp[i][j] = ans
}
}
return dp[N-1][num]
}
func getMin(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
//func min2(arr []int, num int) int {
// if num < 1 || len(arr) < num {
// return 0
// }
// N := len(arr)
// w := make([][]int, N+1)
// for i := 0; i < N+1; i++ {
// w[i] = make([]int, N+1)
// }
// for L := 0; L < N; L++ {
// for R := L + 1; R < N; R++ {
// w[L][R] = w[L][R-1] + arr[R] - arr[(L+R)>>1]
// }
// }
//
// dp := make([][]int, N)
// for i := 0; i < N; i++ {
// dp[i] = make([]int, num+1)
// }
//
// best := make([][]int, N)
// for i := 0; i < N; i++ {
// best[i] = make([]int, num+1)
// }
// for i := 0; i < N; i++ {
// dp[i][1] = w[0][i]
// best[i][1] = -1
// }
// for j := 2; j <= num; j++ {
// for i := N - 1; i >= j; i-- {
// down := best[i][j-1]
// up := twoSelectOne(i == N-1, N-1, best[i+1][j])
// ans := math.MaxInt32
// bestChoose := -1
// for leftEnd := down; leftEnd <= up; leftEnd++ {
// leftCost := twoSelectOne(leftEnd == -1, 0, dp[leftEnd][j-1])
// rightCost := twoSelectOne(leftEnd == i, 0, w[leftEnd+1][i])
// cur := leftCost + rightCost
// if cur <= ans {
// ans = cur
// bestChoose = leftEnd
// }
// }
// dp[i][j] = ans
// best[i][j] = bestChoose
// }
// }
// return dp[N-1][num]
//}
func twoSelectOne(isSelectFirst bool, a int, b int) int {
if isSelectFirst {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- Improving the classification of motor imagery by combining EEG and MEG signals in BCI
- Design of CAN bus controller based on FPGA (Part 2)
- 推荐几款超级实用的数据分析利器
- 一文详解JackSon配置信息
- HMM to CRF understanding and learning notes
- Poor remote code execution in Alien Swarm
- Apple is no match for the longest selling mobile phone made in China, and has finally brought back the face of the domestic mobile phone
- Instruction document for online written examination assistance of smart side school recruitment
- Junit5中的参数化测试(Parameterized Tests)指南
- 如何实现容器内的SqlServer的数据库迁移
猜你喜欢

实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

Using oasis to develop a hop by hop (I) -- Scene Building

高速公路服务区智能一体机解决方案

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

Vim编辑器的最常用的用法

FreeRTOS新建任务不执行问题解决办法

国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子

How to expand disk space on AWS host

The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
随机推荐
Detailed explanation of estab of Stata regression table output
Logging is not as simple as you think
Industry cases of successful digital transformation
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)
I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议
Hardware security threats of cloud infrastructure
还在担心漏测吗?快来使用jacoco统计下代码覆盖率
Mongodb introductory practical tutorial: learning summary directory
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
MySQL development specification
SIGGRAPH 2022 | 真实还原手部肌肉,数字人双手这次有了骨骼、肌肉、皮肤
Two problems of qtreewidget returning as DLL in singleton mode
在Gradle 中对Junit5 测试框架引用
2021-04-22: given many line segments, each line segment has two numbers [start, end],
Istio practical tips: Customize Max_ body_ size
Convert text to hexadecimal, and reverse
不忘初心