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 ai 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)=|al−al+1|+|al+1−al+2|+…+|ar−1−ar|. Here |x−y| is the absolute value of x−y . A group with only one villager has a power of 0 . Kars 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≤r1<r2<…<rk−1<n such that f(1,r1)+f(r1+1,r2)+…+f(rk−1+1,n) is minimised. Help Kars in finding the minimum value of f(1,r1)+f(r1+1,r2)+…+f(rk−1+1,n) . Input The first line contains a single integer t (1≤t≤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≤k≤n≤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 a1,a2,…,an (1≤ai≤500) — the suspicion of each of the villagers. Output 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,r1)+f(r1+1,r2)+…+f(rk−1+1,n) . Example inputCopy 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 outputCopy 4 11 2 Note 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 .
时间: 2024-04-07 07:30:17 浏览: 198
这是一个关于分组问题的题目,题目给出了每个村民的怀疑程度,要求将村民分成 k 个连续的子组,使得他们的总怀疑程度最小。我们可以使用动态规划来解决这个问题。
首先,我们定义一个二维数组 dp,其中 dp[i][j] 表示将前 i 个村民分成 j 个连续子组时的最小怀疑程度。
接下来,我们可以使用状态转移方程来计算 dp[i][j]。假设当前村民为第 i 个,我们可以考虑将其与前面的村民组成一个子组,或者单独作为一个子组。如果将其与前面的村民组成一个子组,则 dp[i][j] = dp[k][j-1] + f(k+1, i),其中 k < i。如果单独作为一个子组,则 dp[i][j] = dp[i-1][j-1] + f(i, i),即当前村民的怀疑程度。我们取这两种情况中的最小值作为 dp[i][j] 的值。
最后,答案就是 dp[n][k]。
下面是一个具体的实现示例:
```cpp
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int solve(vector<int>& suspicions, int n, int k) {
vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));
vector<vector<int>> prefixSum(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++) {
prefixSum[i][i] = suspicions[i - 1];
for (int j = i + 1; j <= n; j++) {
prefixSum[i][j] = prefixSum[i][j - 1] + suspicions[j - 1];
}
}
for (int i = 1; i <= n; i++) {
dp[i][1] = prefixSum[1][i];
}
for (int j = 2; j <= k; j++) {
for (int i = j; i <= n; i++) {
dp[i][j] = INT_MAX;
for (int l = j - 1; l < i; l++) {
dp[i][j] = min(dp[i][j], dp[l][j - 1] + prefixSum[l + 1][i]);
}
}
}
return dp[n][k];
}
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> suspicions(n);
for (int i = 0; i < n; i++) {
cin >> suspicions[i];
}
int result = solve(suspicions, n, k);
cout << result << endl;
}
return 0;
}
```
希望这个解答对你有帮助!如果还有其他问题,请随时问我。
阅读全文
相关推荐


















