You are given an array aa of length nn and an array bb of length nn. The cost of a segment [l,r][l,r], 1≤l≤r≤n1≤l≤r≤n, is defined as |bl−ar|+|br−al||bl−ar|+|br−al|.
Recall that two segments [l1,r1][l1,r1], 1≤l1≤r1≤n1≤l1≤r1≤n, and [l2,r2][l2,r2], 1≤l2≤r2≤n1≤l2≤r2≤n, are non-intersecting if one of the following conditions is satisfied: r1
The length of a segment [l,r][l,r], 1≤l≤r≤n1≤l≤r≤n, is defined as r−l+1r−l+1.
Find the maximum possible sum of costs of non-intersecting segments [lj,rj][lj,rj], 1≤lj≤rj≤n1≤lj≤rj≤n, whose total length is equal to kk.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1000)(1≤t≤1000) — the number of sets of input data. The description of the test cases follows.
The first line of each test case contains two integers nn and kk (1≤k≤n≤30001≤k≤n≤3000) — the length of array aa and the total length of segments.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of array aa.
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (−109≤bi≤109−109≤bi≤109) — the elements of array bb.
It is guaranteed that the sum of nn over all test case does not exceed 30003000.
Output
For each test case, output a single number — the maximum possible sum of costs of such segments.
我比赛时写的是o(nk^2) 的比较常规的转移
dp[n1][k1]=max(dp[n1−1][k1],dp[n1−l][k1−l]+f(n1−l+1,n1),1≤l≤k1)
看别人写之后才发现题目太妙了
首先我们不要把目光光顾着所有个区间,你去看方程的含义
其实化简出来有4中不同的方程
bl-ar+br-al
ar-bl+br-al
ar-bl+al-br
bl-ar+al-br
那bl,al来说有4种不同的状态(1,1),(1,-1),(-1,1),(-1,-1)
因为bl和ar相对应,al和bl相对应,那么br,br的状态恰好和ar,al的相反
所有整个方程有4种状态
那我们怎么知道要用哪种状态呢
我们用dp[4010][4010][5]来存储
dp[i][j][k]--》i表示当前到达第i位,j表示当前取j个元素,k(0~3)用状态压缩表示当第j个元素取al和bl的状态以及前面i-1位中取j-1个元素的最佳状态的和,k(4)表示当前取第i位取j个元素的最佳状态
因为我们上面说你知道ar,al的状态就能知道br,bl的状态:比如bl,al=(1,-1)则br,ar=(1,-1)
因为所有你通过枚举b就能知道当前的最大值
#include
#include
#include//accumulate(be,en,0)
#include//rfind("string"),s.find(string,begin)!=s.npos,find_first _of(),find_last_of()
#include//to_string(value),s.substr(int begin, int length);
#include
#include
#include//res.erase(unique(res.begin(), res.end()), res.end()),reverse(q.begin(),q.end()),vector().swap(at[mx])
#include//priority_queue(big) /priority_queue, greater> q(small)
#include
//#include