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. linux 的 vi 命令详解

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  2. 安装ODOO13

    在CentOS 7服务器下安装和配置Odoo 13 录到服务器: ssh root@your_server_ip1如需检查计算机上安装的CentOS的版本,可以运行以下命令: cat /etc/red ...

  3. 使用 conda 和 Jupyter 创建你的自定义 R 包,转换笔记为幻灯片

    创建你的自定义 R 包 出于用户使用方便考虑,Anaconda 已经在 "R Essentials" 中打包了一些最常用的数据科学 R 包.使用 conda metapackage ...

  4. CSS3+Jquery实现带动画效果的下拉选择框

    CSS3+JQuery实现带动画效果的下拉选择框. 元素结构为: 1 <div class="box"> 2 <p>this is the first li ...

  5. MySQL存储之为什么要使用B+树做为储存结构?

    导言: 在使用MySQL数据库的时候,我们知道了它有两种物理存储结构,hash存储和B+树存储,由于hash存储使用的少,而B+树存储使用的范围就多些,如 InnoDB和MYISAM引擎都是使用的B+ ...

  6. 【AI新趋势期刊#1】GPT自动理解视频、AI法律顾问、大模型安全围栏

    每天都要浏览大量AI相关新闻,是不是感到信息量爆炸,有效信息少? 这么多新产品和新工具,到底哪些是真正是有价值的,哪些只是浮躁的一时热点? 想参与AI产品和工具的开发,从哪里能够获得大量的灵感和思路? ...

  7. SpringBoot定义优雅全局统一Restful API 响应框架完结撒花篇封装starter组件

    之前我们已经,出了一些列文章. 讲解如何封统一全局响应Restful API. 感兴趣的可以看我前面几篇文章 (整个starter项目发展史) SpringBoot定义优雅全局统一Restful AP ...

  8. Linux中常用数据库管理系统之MariaDB

    Linux中常用数据库管理系统之MariaDB 我们生活在信息化时代,经常要跟数据打交道,它在我们的日常生活中无处不在,比如手机支付,微信聊天,淘宝购物,使用的这些在后台都会对应一个叫数据库的存在.数 ...

  9. 浅析华为云Astro的5大关键能力技术

    摘要:本文以技术方案视角,对华为云Astro低代码平台的一些核心功能进行简要介绍. 背景介绍 低代码开发基于可视化开发的概念,结合了云原生和多终端体验技术,它可以在大多数业务场景中,帮助企业显著的提升 ...

  10. 零基础实现Java直播(一):集成

    从泛娱乐的秀场直播,到直播带货潮流中的电商直播,直播已经成为2020年最热的应用技术之一,越来越多的产品希望加入直播功能.下面我们基于即构Express Video SDK,来分享如何从零实现线上直播 ...