牛客多校第一场 E、Removal(dp)

链接:https://www.nowcoder.com/acm/contest/139/E
来源:牛客网
 

题目描述

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.

题目大意:给你一个大小为n的数组,你可以删掉数组中的任意m个数,问你在删除m个数之后剩下的数组有多少种。(其中数组的数的大小<=k)

题目思路:根据叉姐给出的题解,我们可以假设next(i,c)为在第 i 位之后第一个出现 c 这个数的位置,这样在转移的过程中我们只要删除i~next(i,c)这个区间内所有的数,就可以形成一个新的数组了。

我们再假设dp[i][j]为前 i 位数中已经删除了j个数的方案数,这样就可以得到状态转移方程为

dp[next(i,c)][j+next(i,c)-i-1]+=dp[i][j];

最后的答案便为

\sum_{j=0}^{m}dp[n-j][m-j]

(将末尾[n-j+1,n]剩下的 j 个数全部删除之后就满足删除m个数的操作了)

AC代码如下:

#include 
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<pii;
const int MX=1e5+7;
const ll mod=1e9+7;

int n,m,k;
int a[MX],nxt[MX][11];
ll dp[MX][11];

int main(){
    while(~scanf("%d%d%d",&n,&m,&k)){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=k;i++) nxt[n][i]=n+1;
        for(int i=n;i>=1;i--){
            for(int j=1;j<=k;j++) nxt[i-1][j]=nxt[i][j];
            nxt[i-1][a[i]]=i;
        }
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i

 

你可能感兴趣的:(dp,ACM)