牛客网多校训练第一场 E - Removal(线性DP + 重复处理)
链接:
https://www.nowcoder.com/acm/contest/139/E
题意:
给出一个n(1≤n≤1e5)个整数(范围是1至10)的序列,
求从中移除m(1≤m≤min(n-1,10))个整数后不同序列的数量模(1e9+7)。
分析:
设d[i][t]表示当前匹配到了第i个数字,总共删了t个数字时的不同序列的数量。
先不考虑序列重复的情况,
则d[i][t] = d[i-1][t](不删第i个数字)+ d[i-1][t-1](删第i个数字)。
现在考虑减去重复的序列。
设有序列abcdec(以字符串为例),可以发现,s[3]=s[6],当i=6,t=3时,
删除cde与删除dec得到的序列是一样的,都是abc,即匹配到s[6]时产生了重复。
这时减去删除cde的方案数(与其前面序列ab相应的方案数相同)即可。
即d[6][3]减去d[2][0](表示从前面的2个元素中删除0个元素,最后再删除cde的方案数)。
代码:
import java.io.*;
import java.util.*;
import static java.util.Arrays.*; public class Main {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
final int UP = (int)1e5 + 5;
final long MOD = (long)1e9 + 7;
int last[] = new int[10+5]; // last[i]:数字i之前出现的最后位置
long d[][] = new long[UP][10+5]; void MAIN() {
for(int i = 0; i < 10+5; i++) d[i][i] = 1;
for(int i = 0; i < UP; i++) d[i][0] = 1;
while(cin.hasNext()) {
int n = cin.nextInt();
int m = cin.nextInt();
cin.nextInt();
fill(last, 0);
for(int v, i = 1; i <= n; i++) {
v = cin.nextInt();
for(int t = 1; t <= m; t++) {
d[i][t] = (d[i-1][t] + d[i-1][t-1]) % MOD;
if(i - last[v] > t || last[v] == 0) continue; // 当i-last[v]<=t时才会出现重复
d[i][t] = (d[i][t] - d[last[v]-1][t-(i-last[v])] + MOD) % MOD;
}
last[v] = i;
}
System.out.println(d[n][m]);
}
} public static void main(String args[]) { new Main().MAIN(); }
}
牛客网多校训练第一场 E - Removal(线性DP + 重复处理)的更多相关文章
- 牛客网多校训练第一场 I - Substring(后缀数组 + 重复处理)
链接: https://www.nowcoder.com/acm/contest/139/I 题意: 给出一个n(1≤n≤5e4)个字符的字符串s(si ∈ {a,b,c}),求最多可以从n*(n+1 ...
- 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)
链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...
- 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)
链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...
- 牛客网多校训练第一场 D - Two Graphs
链接: https://www.nowcoder.com/acm/contest/139/D 题意: 两个无向简单图都有n(1≤n≤8)个顶点,图G1有m1条边,图G2有m2条边,问G2有多少个子图与 ...
- 牛客网多校训练第一场 B - Symmetric Matrix(dp)
链接: https://www.nowcoder.com/acm/contest/139/B 题意: 求满足以下条件的n*n矩阵A的数量模m:A(i,j) ∈ {0,1,2}, 1≤i,j≤n.A(i ...
- 牛客网多校训练第一场 A - Monotonic Matrix(Lindström–Gessel–Viennot lemma)
链接: https://www.nowcoder.com/acm/contest/139/A 题意: 求满足以下条件的n*m矩阵A的数量模(1e9+7):A(i,j) ∈ {0,1,2}, 1≤i≤n ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
- 牛客网多校训练第九场H Cutting Bamboos
题目链接:https://ac.nowcoder.com/acm/contest/889/H 题意:给出n颗竹子的高度,q次询问,每次询问给出l,r,x,y,每次选取[l,r]中的竹子,砍y次砍掉所有 ...
- 牛客网多校第3场C-shuffle card 平衡树或stl(rope)
链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...
随机推荐
- HDU 5701 ——中位数计数——————【思维题】
中位数计数 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- yum -y update 报错:GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
用的是centos6.5的镜像,yum源太老了,修改了之后想更新一下: yum -y update 执行报错: warning: rpmts_HdrFromFdno: Header V3 RSA/SH ...
- web.coofig 配置跨域问题
<customHeaders> <add name="/> <add name="Access-Control-Allow-Origin" ...
- access 2010
access 2010(表4.1---4.5) 数据表视图创建表:创建----改ID一行的内容(字段----格式----数据类型----格式----字段大小)----完成. 设计视图创建表:创建--- ...
- Node.js学习笔记(二) --- CommonJs和Nodejs 中自定义模块
一. 什么是 CommonJs? JavaScript 是一个强大面向对象语言,它有很多快速高效的解释器. 然而, JavaScript标准定义的 API 是为了构建基于浏览器的应用程序.并没有制定一 ...
- Vue-[v-model]理解示例
对应文档节点: https://vuefe.cn/v2/guide/components.html#Customizing-Component-v-model <body> <div ...
- Be opinionated out of the box but get out of the way quickly as requirements start to diverge from
Be opinionated out of the box but get out of the way quickly as requirements start to diverge from t ...
- 设计模式入门,单件模式,c++代码实现
// test05.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...
- ios的白屏坑
请参考http://www.fly63.com/article/detial/287
- echarts 表格与 div 之间 空白的设置
一. options 图表选项,包含图表实例任何可配置选项: 公共选项 , 组件选项 , 数据选项 {Object} grid 二. grid 直角坐标系内绘图网格 名称 默认值 描述 {number ...