Smiling&Weeping

                     ----总有人间一两风,填我十万八千梦

A. The Man who became a God
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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|.�(�,�)=|��−��+1|+|��+1−��+2|+…+|��−1−��|.

Here |x−y||�−�| is the absolute value of x−y�−�. A group with only one villager has a power of 00.

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�−1 positive integers 1≤r1<r2<…<rk−1<n1≤�1<�2<…<��−1<� such that f(1,r1)+f(r1+1,r2)+…+f(rk−1+1,n)�(1,�1)+�(�1+1,�2)+…+�(��−1+1,�) is minimised. Help Kars in finding the minimum value of f(1,r1)+f(r1+1,r2)+…+f(rk−1+1,n)�(1,�1)+�(�1+1,�2)+…+�(��−1+1,�).

Input

The first line contains a single integer t� (1≤t≤100)(1≤�≤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)(1≤�≤�≤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,�2,…,�� (1≤ai≤500)(1≤��≤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)�(1,�1)+�(�1+1,�2)+…+�(��−1+1,�).

 
 

详见:Problem - A - Codeforces

思路:DP分组,使总数和最小,那么我们就定义状态dp[i][j] 代表前i个中分j组时的最小值

然后写出状态方程:dp[i][j] = min(dp[i][j] , dp[l][j-1]+sum[i]-sum[l+1])

这里呢有一些细节,你如sum[i]-sum[l+1] , dp[l][j-1]已经囊括了a0--al的范围了,并且单独一个村庄power=0,因此对于之后,便不必记录al与al+1的关系了,对了还要注意哦:分成k组,只需要划k-1条线即可,那么我们上代码

 1 #include<bits/stdc++.h>
2 #include<vector>
3 using namespace std;
4 int t , n , k, dp[110][110];
5 int main()
6 {
7 scanf("%d",&t);
8 while(t--)
9 {
10 memset(dp , 0x3f , sizeof(dp));
11 scanf("%d%d",&n,&k);
12 vector<int> sum(n+10);
13 vector<int> a(n+10);
14 for(int i = 1; i <= n; i++)
15 scanf("%d",&a[i]);
16 dp[1][0] = 0; dp[0][0] = 0;
17 for(int i = 2; i <= n; i++)
18 sum[i] = sum[i-1] + abs(a[i]-a[i-1]);
19 for(int i = 1; i <= n; i++)
20 dp[i][0] = sum[i];
21 for(int i = 1; i <= n; i++)
22 for(int j = 1; j <= min(i,k-1); j++)                 // 这里需要注意
23 for(int l = j-1; l < i; l++)
24 dp[i][j] = min(dp[i][j] , dp[l][j-1]+sum[i]-sum[l+1]); // 关键
25 printf("%d\n",dp[n][k-1]);
26 }
27 return 0;
28 }

؏؏ᖗ乛◡乛ᖘ؏؏我们下期再见,(づ ̄3 ̄)づ╭~you

Codeforces Round 882 div.2 A的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. Hugging News #0526: Hugging Cast 发布第一期、邀请来认领自己的论文啦!

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  2. Flutter三棵树系列之BuildOwner

    引言 Flutter开发中三棵树的重要性不言而喻,了解其原理有助于我们开发出性能更优的App,此文主要从源码角度介绍Element树的管理类BuildOwner. 是什么? BuildOwner是el ...

  3. .net开发者应掌握的利器CommunityToolkit.HighPerformance——MemoryOwner与SpanOwner

    MemoryOwner和SpanOwner都可以理解为是对ArrayPool<>的一个包装,无非一个是在堆栈上,一个是在托管堆上.既然做了包装,那肯定随之而来就是改进和优化. 目录 Mem ...

  4. Java设计模式中的几种常用设计模式总结

    一.设计模式概念 1.定义 ​ Java包含23种设计模式,是一套对代码设计经验的总结,被人们反复利用,多人熟知的代码设计方式. 2.目的 ​ 为了提高代码的可读性,可扩展性以及代码的复用性,为了解决 ...

  5. 使用 Docker 分析高通量测序数据

    端午节假期,先祝各位 Bio IT 的爱好者们,节日快乐! 做生信的童鞋想要学习 Docker,或者使用 Docker+Pipeline 封装自己的一套数据分析流程,相信一定不能错过胡博强老师在201 ...

  6. Android APK 文件结构

    序言 APK(全称:Android application package,Android应用程序包)是Android操作系统使用的一种应用程序包文件格式,用于分发和安装移动应用及中间件. APK 文 ...

  7. 解码器 | 基于 Transformers 的编码器-解码器模型

    基于 transformer 的编码器-解码器模型是 表征学习 和 模型架构 这两个领域多年研究成果的结晶.本文简要介绍了神经编码器-解码器模型的历史,更多背景知识,建议读者阅读由 Sebastion ...

  8. shell编程-提取IP地址

    1.使用cut文本处理工具提取 [root@hadoop129 scripts]# ifconfig ens33 | grep netmask | cut -d " " -f 10 ...

  9. GPT3与机器翻译的结合:探索新的语言翻译技术

    目录 引言 随着全球化的加速和人工智能的快速发展,机器翻译成为了许多企业.机构和个人的痛点.虽然已有多种机器翻译技术,但基于自然语言处理和深度学习的机器翻译一直缺乏有效的解决方案,这导致机器翻译的准确 ...

  10. 使用Stable Diffusion生成艺术二维码

    在数字艺术的世界中,二维码已经从单纯的信息承载工具转变为可以展示艺术表达的媒介.这是通过使用Stable Diffusion的技术实现的,它可以将任何二维码转化为独特的艺术作品.接下来,我们将一步步教 ...