Codeforces 958 C2. Encryption (medium)(dp)

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的情况.

### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值