HDU 6078 Wavel Sequence (dp)
Description
Have you ever seen the wave? It’s a wonderful view of nature. Little Q is attracted to such wonderful thing, he even likes everything that looks like wave. Formally, he defines a sequence a1,a2,…,an as ”wavel” if and only if a1 < a2 > a3 < a4 > a5 < a6…
Now given two sequences a1,a2,…,an and b1,b2,…,bm, Little Q wants to find two sequences f1,f2,…,fk(1≤fi≤n,fi
Input
The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are 2 integers n,m(1≤n,m≤2000) in the first line, denoting the length of a and b.
In the next line, there are n integers a1,a2,…,an(1≤ai≤2000), denoting the sequence a.
Then in the next line, there are m integers b1,b2,…,bm(1≤bi≤2000), denoting the sequence b.
Output
For each test case, print a single line containing an integer, denoting the answer. Since the answer may be very large, please print the answer modulo 998244353.
Sample Input
1
3 5
1 5 3
4 1 1 5 3
Sample Output
10
题意
定义波浪序列为满足 a1< a2 > a3 < a4 … 的序列,现给出两个数组 a 和 b ,从 a 中选出满足波浪序列的一个子序列 f , b 中选出满足波浪序列的子序列 g ,求有多少种选法满足 f = g 。
思路
dp[0][j]
代表以 b[j]
结尾且最后为波谷的情况数目。
dp[1][j]
代表以 b[j]
结尾且最后为波峰的情况数目。
显然,结尾为波谷的情况可以由 波峰 + 一个小的数 转移而来,而结尾为波峰的情况可以由 波谷 + 一个大的数 转移而来。
因此我们定义 ans0、ans1
分别表示在该轮中相对于 a[i]
来说 b[j]
可作为波谷与波峰的数目。
枚举每一个 a[i]
,并且判断其与 b[j]
的大小关系:
- 若
a[i] < b[j]
,则说明b[j]
可作为一个波峰出现 - 若
a[i] > b[j]
,则说明b[j]
可作为一个波谷出现 - 若
a[i] = b[j]
,则说明找到一个对f = g
有贡献的值,更新答案
AC 代码
#include<bits/stdc++.h>
using namespace std;typedef __int64 LL;const int mod = 998244353;
const int maxn = 2100;int a[maxn],b[maxn];
LL dp[2][maxn];
int main()
{int T;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);for(int i=0; i<n; i++)scanf("%d",a+i);for(int i=0; i<m; i++)scanf("%d",b+i);memset(dp,0,sizeof(dp));LL ans=0;for(int i=0; i<n; i++){LL ans1=1,ans0=0; // 该轮最后一个为波峰/波谷的数量for(int j=0; j<m; j++){if(a[i]==b[j]) // 当前位有两种状态(波峰、波谷),可分别由之前的波谷、波峰转移而来{dp[1][j]+=ans0;dp[0][j]+=ans1;ans=(ans+ans0+ans1)%mod;}else if(a[i]<b[j]) // b[j] 可相对于 a[i] 做波峰ans1=(ans1+dp[1][j])%mod;else ans0=(ans0+dp[0][j])%mod; // 反之亦然}}printf("%I64d\n",ans);}return 0;
}
HDU 6078 Wavel Sequence (dp)相关推荐
- HDU 6078 Wavel Sequence(dp)
Description 定义波浪序列:a1 > a2 < a3-,现在给出一个长度为n的序列a和一个长度为m的序列b,求a和b的公共波浪子序列个数 Input 第一行一整数T表示用例组数, ...
- HDU - 6078 Wavel Sequence(动态规划+时间优化)
代码: #include<bits/stdc++.h> #define MOD 998244353 using namespace std; #define MAXN 2050int dp ...
- 【XSY2564】sequence(dp)
题面 [题目描述] 给定一个长度为nnn的由['0'...'9'][\text{'}0\text{'}...\text{'}9\text{'}]['0'...'9']组成的字符串sss,v[i,j]v ...
- HDU 6078 Wavel Sequence
Wavel Sequence Problem Description Have you ever seen the wave? It's a wonderful view of nature. Lit ...
- 2017多校第4场 HDU 6078 Wavel Sequence DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:求两个序列的公共波形子序列的个数. 解法: 类似于最长公共上升子序列,对于每个i,只考虑存 ...
- (2017多校训练第四场)HDU - 6078 Wavel Sequence dp
传送门:点击打开链接 定义状态dp[i][j][0]表示以a[i],b[j]结尾的且为波谷的情况总和,dp[i][j][1] 为波峰. 对于某个i,j满足a[i] == b[j],则dp[i][j][ ...
- HDU 6078 Wavel Sequence【动态规划】
题目来戳呀 Problem Description Have you ever seen the wave? It's a wonderful view of nature. Little Q is ...
- hdu 6078 Wavel Sequence
题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6078 (2017 Multi-University Training Contest - Team ...
- HDOJ-2062 :Subset sequence(DP)
题目:求子集序列 Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,3}. A subset sequen ...
最新文章
- mysql json类型数组索引_MySQL JSON 类型数据操作
- 把磁力下载站改为python系统
- layer的一种用法,页面多选时用地址传递值过多时,用弹出层画一个form表单,然后通过提交表单传值
- What you should know about .so files
- ZOJ-3494 BCD Code (ac自动机+数位dp)
- 深度学习简介(一)——卷积神经网络
- 漫步最优化三十六——基本共轭方向法
- Ubuntu下安装Balsamiq Mockups
- Hadoop入门基础教程 Hadoop之完全分布式环境搭建
- linux ctrl c 子进程,ctrl c会向Linux中的父进程和子进程发送SIGINT信号吗?
- ADS实验报告二:滤波器的仿真设计
- Unity-世界坐标与屏幕坐标
- C# 图片验证码简单例子
- 找不到项目 该项不在计算机中,Win7提示“找不到该项目”怎么办 Win7提示“找不到该项目”解决方法...
- 浅析ip地址的分类及地址范围
- Spring Data JPA 之 @Entity 的常用注解
- 举个栗子!Tableau 技巧(90):让你的图表背景变透明
- 【书籍翻译笔记】Next Generation Wireless LANs 802.11n and 802.11ac: Chapter1
- 支持使用vlc在chrome等各种浏览器打开rtmp rtsp 谷歌浏览器Chrome播放rtsp视频流解决方案
- 偏爱小市值策略的,进来了解下这个策略