A15984 | The Man who became a God
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Kars is tired and resentful of the narrow mindset of his village since they are content with staying where they are and are not trying to become the perfect life form. Being a top-notch inventor, Kars wishes to enhance his body and become the perfect life form. Unfortunately, $n$ of the villagers have become suspicious of his ideas. The $i$ -th villager has a suspicion of $a_i$ on him. Individually each villager is scared of Kars, so they form into groups to be more powerful.
The power of the group of villagers from $l$ to $r$ be defined as $f(l,r)$ where
$$$$f(l,r) = |a_l - a_{l+1}| + |a_{l + 1} - a_{l + 2}| + \ldots + |a_{r-1} - a_r|. $$ </p><p>Here $|x-y|$ is the absolute value of $x-y$ . A group with only one villager has a power of $0$ .</p><p><span class="tex-font-style-it">Kars</span> wants to break the villagers into exactly $k$ contiguous subgroups so that the sum of their power is minimized. Formally, he must find $k - 1$ positive integers $1 \\le r\_1 < r\_2 < \\ldots < r\_{k - 1} < n$ such that $f(1, r\_1) + f(r\_1 + 1, r\_2) + \\ldots + f(r\_{k-1} + 1, n)$ is minimised. Help <span class="tex-font-style-it">Kars</span> in finding the minimum value of $f(1, r\_1) + f(r\_1 + 1, r\_2) + \\ldots + f(r\_{k-1} + 1, n)$$$.
The power of the group of villagers from $l$ to $r$ be defined as $f(l,r)$ where
$$$$f(l,r) = |a_l - a_{l+1}| + |a_{l + 1} - a_{l + 2}| + \ldots + |a_{r-1} - a_r|. $$ </p><p>Here $|x-y|$ is the absolute value of $x-y$ . A group with only one villager has a power of $0$ .</p><p><span class="tex-font-style-it">Kars</span> wants to break the villagers into exactly $k$ contiguous subgroups so that the sum of their power is minimized. Formally, he must find $k - 1$ positive integers $1 \\le r\_1 < r\_2 < \\ldots < r\_{k - 1} < n$ such that $f(1, r\_1) + f(r\_1 + 1, r\_2) + \\ldots + f(r\_{k-1} + 1, n)$ is minimised. Help <span class="tex-font-style-it">Kars</span> in finding the minimum value of $f(1, r\_1) + f(r\_1 + 1, r\_2) + \\ldots + f(r\_{k-1} + 1, n)$$$.
输入格式
The first line contains a single integer $t$ $(1 \leq t \leq 100)$ — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers $n,k$ $(1 \leq k \leq n \leq 100)$ — the number of villagers and the number of groups they must be split into.
The second line of each test case contains $n$ integers $a_1,a_2, \ldots, a_n$ $(1 \leq a_i \leq 500)$ — the suspicion of each of the villagers.
The first line of each test case contains two integers $n,k$ $(1 \leq k \leq n \leq 100)$ — the number of villagers and the number of groups they must be split into.
The second line of each test case contains $n$ integers $a_1,a_2, \ldots, a_n$ $(1 \leq a_i \leq 500)$ — the suspicion of each of the villagers.
输出格式
For each test case, output a single integer — the minimum possible value of sum of power of all the groups i. e. the minimum possible value of $f(1,r_1) + f(r_1 + 1, r_2) + \ldots + f(r_{k-1} + 1, n)$ .
输入输出样例
输入 #1
3 4 2 1 3 5 2 6 3 1 9 12 4 7 2 12 8 1 9 8 2 3 3 1 8 7 7 9 2
输出 #1
4 11 2
In the first test case, we will group the villagers with suspicion $(1,3,5,2)$ into $(1,3,5)$ and $(2)$ . So, $f(1,3) + f(4,4) = (|1 - 3| + |3 - 5|) + 0 = 4 + 0 = 4$ .
In the second test case, we will group the villagers with suspicion $(1,9,12,4,7,2)$ into $(1),(9,12),(4,7,2)$ . So, $f(1,1) + f(2,3) + f(4,6) = 0 + 3 + 8 = 11$ .
In the second test case, we will group the villagers with suspicion $(1,9,12,4,7,2)$ into $(1),(9,12),(4,7,2)$ . So, $f(1,1) + f(2,3) + f(4,6) = 0 + 3 + 8 = 11$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted