当前位置:网站首页>2022-04-20: the small regiment goes to participate in the military training. The military training is coming to an end. The officer wants to divide n people in a row into m groups, and then ask each g
2022-04-20: the small regiment goes to participate in the military training. The military training is coming to an end. The officer wants to divide n people in a row into m groups, and then ask each g
2022-04-21 07:50:00 【One question per day for frame constructor of Fuda University】
2022-04-20: Small regiments go to military training , Military training is coming to an end ,
Sir wants to put everyone in a row n Personal share m Group , Then let each group go to the parade ceremony ,
Only one group of adjacent people can be selected , You can't change the position of people in the team at will ,
Scores will be given at the military parade , One of the strange deduction points is the maximum difference between each group ,
That is, the maximum value of each group minus the minimum value ,
Sir wants this to be divided into m Minimum total deduction component , Is this m The sum of the range difference of each group is the smallest .
The officer is thinking about how to arrange , Let Xiaotuan help him .
answer 2022-04-20:
Dynamic programming .
Time complexity :O(M * N * N).
The code to use rust To write . The code is as follows :
use rand::Rng;
fn main() {
let mut arr: Vec<isize> = vec![];
let n = rand::thread_rng().gen_range(10, 30);
println!("n = {}", n);
for i in 0..n {
arr.push(rand::thread_rng().gen_range(1, 1000));
}
println!("arr = {:?}", arr);
let m = rand::thread_rng().gen_range(1, n);
println!("m = {}", m);
let ret = min_score2(&mut arr, m);
println!("ret = {}", ret);
}
fn min_score2(arr: &mut Vec<isize>, m: isize) -> isize {
if m == 0 {
return 0;
}
let n: isize = arr.len() as isize;
let mut score: Vec<Vec<isize>> = vec![];
for i in 0..n {
score.push(vec![]);
for j in 0..n {
score[i as usize].push(0);
}
}
for i in 0..n {
let mut max = arr[i as usize];
let mut min = arr[i as usize];
score[i as usize][i as usize] = max - min;
for j in i + 1..n {
max = get_max(max, arr[j as usize]);
min = get_min(min, arr[j as usize]);
score[i as usize][j as usize] = max - min;
}
}
let mut dp: Vec<Vec<isize>> = vec![];
for i in 0..m + 1 {
dp.push(vec![]);
for j in 0..n {
dp[i as usize].push(0);
}
}
for i in 0..n {
dp[1][i as usize] = score[0][i as usize];
}
for split in 2..=m {
for i in split..n {
dp[split as usize][i as usize] = dp[(split - 1) as usize][i as usize];
for j in 1..=i {
dp[split as usize][i as usize] = get_min(
dp[split as usize][i as usize],
dp[(split - 1) as usize][(j - 1) as usize] + score[j as usize][i as usize],
);
}
}
}
//println!("dp = {:?}", dp);
return dp[m as usize][(n - 1) as usize];
}
fn get_max(a: isize, b: isize) -> isize {
if a > b {
a
} else {
b
}
}
fn get_min(a: isize, b: isize) -> isize {
if a < b {
a
} else {
b
}
}
The results are as follows :

版权声明
本文为[One question per day for frame constructor of Fuda University]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210638180934.html
边栏推荐
- @Slf4j注解中 log 报错
- Leetcode 704 · binary search
- oracle 管理之《sql命令》
- Dynamically generate three-level menu
- 分布式事务Seata
- 服务器基本的安全防护设置
- SQL command of Oracle management
- GoLang项目开发基础
- asp. Net re regular expression, filter the amount of time order number in the returned string, and convert the non-standard time format into the correct time format
- 为什么mysql里面设置符合主键它显示我错了?
猜你喜欢

2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中

Bluetooth Profile Specification之(AVRCP篇)5.1AVCTP的连接和释放

PLSQL developer 14 installation details

fiddler调换字体后界面缺少 恢复

MySQL5.7安装操作手册(Centos7)

2021-10-17

C#中listView列自动适应缩放的完美效果

Studio3t 过期激活办法/以及重新设置使用日期的脚本不可用解决办法/Studio 3T无限激活原创

GoLang学习资源清单
![Navicat even Oracle reports an error [no matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat even Oracle reports an error [no matching authentication protocol]
随机推荐
Solution to red flag with @ Autowired annotation in idea
Text Templates
星际迷航-发现号-第三季最后一集
Supplément à la fonction d'annotation
fiddler调换字体后界面缺少 恢复
Bluetooth Profile Specification之(AVRCP篇)5.1AVCTP的连接和释放
压缩毕业时间到20岁以前,5岁上小学上5年,提高人口数量
Axure产品原型工具使用笔记
批处理解析域名的ip地址并打开网页
div点击折叠收缩
虚拟机安装kali 时的默认密码(官网vmx 文件源)
钉钉自定义机器人 对接源码
Leetcode 707 Design linked list
禁用谷歌跨域的一个办法
瑞萨IDE:CS+ for CC进行BootLoader升级时开发环境配置
C# WebService 接口 通过Request请求获取json参数
Zephyr物联网操作系统专栏汇总
php Rsa加密
343. 求分解整数的乘积最大化Integer Break
.net core 将错误抛出写入.txt文件