牛客多校第一场 E 题 Removal dp

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述 
Bobo has a sequence of integers s1, s2, ..., sn where 1 ≤ si ≤ k.
Find out the number of distinct sequences modulo (109+7) after removing exactly m elements.
输入描述:
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains three integers n, m and k.
The second line contains n integers s1, s2, ..., sn.
输出描述:
For each test case, print an integer which denotes the result.
示例1
输入
复制
3 2 2
1 2 1
4 2 2
1 2 1 2
输出
复制
2
4
备注:
* 1 ≤ n ≤ 105
* 1 ≤ m ≤ min{n - 1, 10}
* 1 ≤ k ≤ 10
* 1 ≤ si ≤ k
* The sum of n does not exceed 106.

题意: 删除m个数字之后有最多多少种不同的序列?

思路: 我们定义 dp[i][j][k]  (i<=n,j<=m,k<=1 )  表示在前i个数字中删除 j 个数字 当前位删不删(不删k 为0 删 为1)的 最大序列数,但是这里有一个去重的问题  就比如  我们 第一个样例。 删除两个,我们删除前两个和后两个剩下的都是 1 。这里我用的办法就是如果两个相同的数字之间的数字全部删除并且 要删除其中的一个,那么我保留左边的,删掉右边的。判断方法就是代码中if中的语句。

代码: 

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N =1e5+5;
ll dp[N][15][2];
int n,m,k;

int pre[N];
int a[N];
int mp[N];

int main()
{
    while(scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }

    for(int i=0;i<=n;i++) pre[i]=-1;
    for(int i=0;i<=k;i++) mp[i]=-1;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++) dp[i][j][0]=dp[i][j][1]=0;
    }

    for(int i=1;i<=n;i++){
        if(mp[a[i]]==-1){
            mp[a[i]]=i;
            continue;
        }
        else{
            pre[i]=mp[a[i]];
            mp[a[i]]=i;
        }
    }

    //
    dp[0][0][0]=1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=m;j++){
            dp[i][j][0]=(dp[i][j][0]+dp[i-1][j][0])%mod;
                dp[i][j][0]=(dp[i][j][0]+dp[i-1][j][1])%mod;
                dp[i][j][1]=(dp[i][j][1]+dp[i-1][j-1][0])%mod;
                dp[i][j][1]=(dp[i][j][1]+dp[i-1][j-1][1])%mod;
                if(pre[i]==-1) continue;
                int len=i-pre[i]-1;
                if(len>=0&&i-len-1>=1&&j-len>=0){
                    dp[i][j][0]=(dp[i][j][0]-dp[i-len-1][j-len][1]+mod)%mod;
                }
        }
    }

    ll ans=(dp[n][m][0]+dp[n][m][1])%mod;
    printf("%lld\n",ans);

    }



    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81162404