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

24 篇文章 0 订阅

链接: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 <bits/stdc++.h>
#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<<"["<<x<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>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<n;i++){
            for(int j=0;j<=m;j++){
                for(int l=1;l<=k;l++){
                    int n1=nxt[i][l],n2=n1-i-1;
                    if(n2+j<=m)
                        dp[n1][j+n2]=(dp[n1][j+n2]+dp[i][j])%mod;
                }
            }
        }
        /*for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++)
                printf("i=%d j=%d dp=%d\n",i,j,dp[i][j]);
        }*/
        ll ans=0;
        for(int j=0;j<=m;j++)
            ans=(ans+dp[n-j][m-j]+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值