Description
Heidi has now broken the first level of encryption of the Death Star plans, and is staring at the screen presenting her with the description of the next code she has to enter. It looks surprisingly similar to the first one – seems like the Empire engineers were quite lazy…
Heidi is once again given a sequence A, but now she is also given two integers k and p. She needs to find out what the encryption key S is.
Let X be a sequence of integers, and p a positive integer. We define the score of X to be the sum of the elements of X modulo p.
Heidi is given a sequence A that consists of N integers, and also given integers k and p. Her goal is to split A into k part such that:
Each part contains at least 1 element of A, and each part consists of contiguous elements of A.
No two parts overlap.
The total sum S of the scores of those parts is maximized.
Output the sum S – the encryption code.
Input
The first line of the input contains three space-separated integer N, k and p (k ≤ N ≤ 20 000, 2 ≤ k ≤ 50, 2 ≤ p ≤ 100) – the number of elements in A, the number of parts A should be split into, and the modulo for computing scores, respectively.
The second line contains N space-separated integers that are the elements of A. Each integer is from the interval [1, 1 000 000].
Output
Output the number S as described in the problem statement.
Examples
input
4 3 10
3 4 7 2
output
16
input
10 5 12
16 3 24 13 9 8 7 5 12 12
output
37
题目描述
将给定的 n 个数字分成 k 个连续的片段,每一段分别求和%p再相加,求出最大值.
解题思路
dp[i][ki][pi]代表前i个数字分成k段且最后一段和为pi 的最大值,premaxx[i][ki]代表前 i 个数字分成 j 段的最大值.
显然,premaxx[i][ki]=max(dp[i][ki][pi]),0<=pi< p.
dp[i][ki][pi]可通过两种状态之间的转化来求得,假设当前遍历到了第i个数字:
第一种,第i个数字可以和dp[i-1][ki][pi]的最后一段合并成一段,则pi’=(pi+a[i])%p,所以dp[i][ki][pi’]=dp[i-1][ki][pi]-pi+pi’.
第二种,第i个数字可以独自构成一段,则dp[i][ki][a[i]]=premaxx[i-1][ki-1]+a[i].
根据数组的含义可知premaxx[n][k]即为所求答案.
代码实现
#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
const int maxn = 2e4+7;
int a[maxn];
int dp[2][57][107];
int premaxx[maxn][57];
int n,k,p;
void solve()
{
for(int i=1; i<=n; i++)
premaxx[i][1]=(premaxx[i-1][1]+a[i])%p;
dp[1][1][a[1]]=a[1];
for(int i=1; i<=n; i++)
{
memset(dp[i%2],0,sizeof(dp[i%2]));
for(int ki=2; ki<=k; ki++)
{
if(ki>i) continue;
for(int pi=0; pi<p; pi++)
{
int pt=dp[(i-1)%2][ki][pi]-pi+(pi+a[i])%p;
int px=(pi+a[i])%p;
dp[i%2][ki][px]=max(dp[i%2][ki][px],pt);
premaxx[i][ki]=max(premaxx[i][ki],dp[i%2][ki][px]);
dp[i%2][ki][a[i]]=max(dp[i%2][ki][a[i]],premaxx[(i-1)][ki-1]+a[i]);
premaxx[i][ki]=max(premaxx[i][ki],dp[i%2][ki][a[i]]);
}
}
}
}
int main()
{
IO;
cin>>n>>k>>p;
for(int i=1; i<=n; i++)
{
cin>>a[i];
a[i]%=p;
}
solve();
cout<<premaxx[n][k]<<endl;
return 0;
}
PS(一个漫长的调BUG的过程):
原代码:
dp[1][1][a[1]]=a[1];
premaxx[1][1]=a[1];
for(ll i=2; i<=n; i++)
{
memset(dp[i%2],0,sizeof(dp[i%2]));
memset(premaxx[i%2],0,sizeof(premaxx[i%2]));
for(ll ki=1; ki<=k; ki++)
{
if(ki>i) continue;
for(ll pi=0; pi<p; pi++)
{
ll pt=dp[(i-1)%2][ki][pi]-pi+(pi+a[i])%p;
ll px=(pi+a[i])%p;
dp[i%2][ki][px]=max(dp[i%2][ki][px],pt);
premaxx[i%2][ki]=max(premaxx[i%2][ki],dp[i%2][ki][px]);
dp[i%2][ki][a[i]]=max(dp[i%2][ki][a[i]],premaxx[(i-1)%2][ki-1]+a[i]);
premaxx[i%2][ki]=max(premaxx[i%2][ki],dp[i%2][ki][a[i]]);
}
}
}
这时我的premaxx还是一个滚动数组,然而!
必须要预先处理出premaxx[i][1] ( 即ki=1)的值,滚动数组美梦破裂(ಥ﹏ಥ),原因是!
对于数据
4 3 10
3 4 7 2
当i=2,ki=1时,当pi=0时会出现pt=dp[1][1][0]-0+(0+4)%p=4,即dp[2][1][4]=4的情况,显然是不正确,忽略掉了第一个数字的存在. 这样预处理之后就可以避免这种情况.
当然这种情况在 ki>1的时候同样存在,比如i=3,ki=2,pi=0,会出现pt=dp[2][2][0]-0+(0+7)%p=7,即dp[3][2][7]=7的情况,显然忽略了前两个数字,但是要知道dp[][][]的作用是什么,这个数组是作为一个过渡来求premaxx[][]的,由于premaxx[i][ki]的最终值一定比前i项任一项的值都大,所以这种情况不会影响到最终的结果.相比之下,dp[i][1][]的值对premaxx[][]的影响不是绝对可以忽略的,单独一项的值可能大于多项相加取余的值,所以必须要预处理ki=1的情况.