Codeforces Round 882 div.2 A
Smiling&Weeping
----总有人间一两风,填我十万八千梦
1 second
256 megabytes
standard input
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
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,�).
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.
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,�).
思路: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的更多相关文章
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- javascript5 定时器功能
定时器功能: 定时器功能是window对象方法,涉及到 定时器和延时器,具体 看代码 定时器 timer=setInterval(function (){},300) 清除定时器: clearInte ...
- Flutter调优--深入探究MediaQuery引起界面Rebuild的原因及解决办法
前言 我们可以通过MediaQuery.of(context)方法获取到一些设备和系统的相关信息,比如状态栏的高度.当前是否是黑暗模式等等,使用起来相当方便,但是也要注意可能引起的页面rebuild问 ...
- 远程挂载 NFS 共享目录引发死机问题
集群的存储空间有限,把一些历史的归档数据放在了公司的另外一台老旧存储服务器上,并使用 NFS 把它挂载到了 log 节点.周末的时候机房空调故障,旧存储服务器挂掉了!周一上班,在集群登陆节点使用df ...
- 信息收集_网络扫描_nmap
信息收集_网络扫描nmap 目标说明 -iL <inputname> (从列表或文件输入) -iR <hostnum> (随机选择生成目标数量) --exclude <h ...
- java接口返回图片链接或pdf链接如何设置在线预览还是下载
之前文章说到了如何通过将文件转成图片或者pdf来实现在线预览,一般来说在线预览图片或者pdf都是存储在图片服务器上的,在通过接口调用把文件返回给前端,但是把文件返回给前端效果一般是有两种:在线预览和下 ...
- IcedID恶意文档钓鱼手法剖析
析 利用oletools静态分析,提取宏代码,如图: Function contents() With ActiveDocument.Content.Find loveDoor = .Execute( ...
- 【序列化与反序列化】关于序列化与反序列化MessagePack的实践
在进行序列化操作之前,我们还对系统进行压测,通过jvisualvm分析cpu,线程,垃圾回收情况等:运用火焰图async-profiler分析系统性能,找出程序中占用CPU资源时间最长的代码块. 代码 ...
- React中编写操作树形数据的自定义Hook
什么是 Hook hook 即为钩子,是一种特殊的函数,它可以让你在函数式组件中使用一些 react 特性,目前在 react 中常用的 hook 有以下几类 useState: 用于在函数组件中定义 ...
- 《Among Us》火爆全球,实时语音助力派对游戏开启第二春
今年在全球"宅经济"的影响下,社交派对类游戏意外的迎来了爆发. 8月份,<糖豆人:终极淘汰赛>突然爆火,创造了首日150万玩家.首周Steam 200万销量.单周Twi ...
- 现代C++(Modern C++)基本用法实践:五、智能指针
概述 c++效率较高的一个原因是我们可以自己定制策略手动申请和释放内存,当然,也伴随着开发效率降低和内存泄漏的风险.为了减少手动管理内存带来的困扰,c++提出了智能指针,可以帮助我们进行内存管理,有三 ...