2021牛客国庆集训派对day1 -Removal(动态规划)

Removal

时间限制: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.
const int MAX=1010;
const int MOD=1e9;
int n,m,k;
int a[1000010];
int pre[100010];
int id[1000010];
int dp[MAX][20]; //dp[i][j]是指到第i位的已经删除了j位数字
//但是要注意当前位置的数字在之前已经删除过了 所以要判断当前位置的i-pre是否大于j
int main(){
    while(cin>>n>>m>>k){
        mms(pre,0);
        mms(id,0);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            pre[i]=id[a[i]];
            id[a[i]]=i;
        }
        mms(dp,0);
        for(int i=0;i<=m;i++){
            dp[i][i]=1;
        }
        for(int i=1;i<=n;i++){
            int d=i-pre[i];
            dp[i][0]=1;
            for(int j=1;j<=m;j++){
                if(j>i)break;
                dp[i][j]=((dp[i-1][j-1]+dp[i-1][j])%MOD+MOD)%MOD;
                if(pre[i]!=0&&d<=j){
                    dp[i][j]=((dp[i][j]-dp[pre[i]-1][j-d])%MOD+MOD)%MOD;
                }
            }
        }
        cout<<dp[n][m]<<endl;
        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值