首页 > 其他分享 >Codeforces Round 882 div.2 A

Codeforces Round 882 div.2 A

时间:2023-07-19 21:11:27浏览次数:47  
标签:882 sum Codeforces al test each div.2 villagers dp

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

 

标签:882,sum,Codeforces,al,test,each,div.2,villagers,dp
From: https://www.cnblogs.com/smiling-weeping-zhr/p/17566758.html

相关文章

  • Codeforces Round 885
    目录写在前面ABCDEF写在最后写在前面比赛地址:https://codeforces.com/contest/1848。我现在手里有三套题要补呃呃这套是两天前补的了,所以简单写写。太好玩辣,多校!A考虑一个2x2的矩阵,若小波奇和她的辣妹朋友位于对角线上就会被辣妹朋友堵在墙角,若相邻则永远抓不到。发现移......
  • Codeforces Round #885 (Div. 2) A-D
    比赛链接A代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;boolsolve(){intn,m,k;cin>>n>>m>>k;intx,y;cin>>x>>y;boolok=1;for(inti=1;i<=k;i++)......
  • Codeforces Round 885 (Div. 2)
    A.VikaandHerFriends枚举所有的点,判断是否存在点与Vika的距离和其他k个人的距离的奇偶性不同。#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintmod=998244353;voidsolve(){intn,m,k,sx,sy;cin>>n>>m>>k>......
  • CodeForces 1848E Vika and Stone Skipping
    洛谷传送门CF传送门感觉比这场的F简单。发现我们要进行\(x\)不断乘一些数然后求答案的操作,猜测答案与\(x\)的因数有关。如果你对数字比较敏感,不难发现答案就是\(x\)的奇因子个数。官方题解的证明:设\(x=f+(f-1)+\cdots+(f-(c-1))\),由等差数列求和公......
  • Codeforces Round 885 (Div. 2) A-D
    A.VikaandHerFriends题意:有一个n*m大小的矩阵,vika在点(a,b),她的k个朋友在分别(xi,yi),所有人每分钟都可以上下左右走一格,每一分钟vika先走,她的朋友后走,不能不走,问vika能否躲过朋友。Solution结论题,只要有一个朋友和她的距离是奇数,那么她肯定会被追上。voidsolve(){ int......
  • Codeforces Round #885(Div. 2)C
    C.维卡和价格标签每个测试的时间限制为1秒每个测试的内存限制为256兆字节输入:标准输入输出:标准输出维卡来到她最喜欢的化妆品店"GoldenPear"。她注意到n个物品的价格自她上次光顾以来发生了变化。她决定分析价格的变化,并计算每个物品的旧价格和新价格之间的差异。维卡喜......
  • Codeforces Round #885 (Div.2) Editorial
    B-VikaandtheBridge题意:从桥的一边走到另一边,每次只能踩在相同颜色的木板上,并且有一次操作,可以修改期中一个模板的颜色。问那种走法,跨过模板的最大值最小。思路:首先可以统计出选择每种颜色的,跳过木板的的个数,如果不能修改颜色,那么答案一定是每个颜色所对应的最大值的最小......
  • Codeforces Round 883 (Div. 3)
    只写部分题目。A.RudolphandCuttheRope#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=2e5+5;intt,n,a[N],b[N];signedmain(){ cin>>t; while(t--){ cin>>n; intres=0; for(inti=......
  • CodeForces 1844C Particles
    洛谷传送门CF传送门原题是[ARC092E]BothSidesMerger。Keyobservation:每个元素的下标奇偶性不改变。于是讨论最后一个数是下标为奇数还是偶数加起来的数。将下标奇偶性相同的元素分开考虑。对于下标奇偶性相同的元素,不难发现答案的上界是所有\(>0\)的元素之和(没有\(>......
  • Educational Codeforces Round 33 (Rated for Div. 2)
    EducationalCodeforcesRound33(RatedforDiv.2)https://codeforces.com/contest/893昨日vp,今日补完FD贪心,思路值得学习;E组合数学推式子,式子不难,关键在于模型抽象F主席树,调了老半天,关键在于要理解查询函数的含义,确定什么时候能返回。A.ChessForThree居然卡了快十分......