题目来戳呀

Problem 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<fi+1) and g1,g2,…,gk(1≤gi≤m,gi<gi+1), where afi=bgi always holds and sequence af1,af2,…,afk is ”wavel”.

Moreover, Little Q is wondering how many such two sequences f and g he can find. Please write a program to help him figure out the answer.

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

Hint

(1)f=(1),g=(2).
(2)f=(1),g=(3).
(3)f=(2),g=(4).
(4)f=(3),g=(5).
(5)f=(1,2),g=(2,4).
(6)f=(1,2),g=(3,4).
(7)f=(1,3),g=(2,5).
(8)f=(1,3),g=(3,5).
(9)f=(1,2,3),g=(2,4,5).
(10)f=(1,2,3),g=(3,4,5).

Source

2017 Multi-University Training Contest - Team 4

题意:

给出a,b数列,定义波浪数列为a1<a2>a3<a4>a5 <a6…(第一个数必须小于第二个数)。
现在找出f,g,使得 afi a_{fi}= bgi b_{gi},同时 afi a_{fi}为波浪序列,求满足此映射关系的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 有贡献的值,更新答案
想法来自这里 讲的真的清楚

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p=998244353;
int a[2005],b[2005];
ll dp[2][2005];
int main()
{int t;scanf("%d",&t);while (t--){int n,m;ll sum=0;memset(dp,0,sizeof(dp));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]);for (int i=0; i<n; i++){ll ans1=1;      ///统计 此数为波峰 波浪数ll ans0=0;      ///统计 此数为波谷 波浪数for (int j=0; j<m; j++){if (a[i]==b[j]) ///相等更新对应波峰波谷的值{dp[0][j]+=ans1;///当前数作为波峰接在波谷之后形成的波浪数dp[1][j]+=ans0;///当前数作为波谷接在波峰之后形成的波浪数sum=(sum+ans1+ans0)%p;  ///总情况数}else if(a[i]> b[j])   ///b小,可作为波谷接在波峰之后形成波浪{ans0+=dp[0][j];ans0%=p;}else{ans1+=dp[1][j];  ///b大,可作为波峰接在波谷之后形成波浪ans1%=p;}}}printf("%lld\n",sum);}return 0;
}

ps:攒了一堆博客没写呜呜呜

HDU 6078 Wavel Sequence【动态规划】相关推荐

  1. HDU 6078 Wavel Sequence

    Wavel Sequence Problem Description Have you ever seen the wave? It's a wonderful view of nature. Lit ...

  2. HDU - 6078 Wavel Sequence(动态规划+时间优化)

    代码: #include<bits/stdc++.h> #define MOD 998244353 using namespace std; #define MAXN 2050int dp ...

  3. HDU 6078 Wavel Sequence (dp)

    Description Have you ever seen the wave? It's a wonderful view of nature. Little Q is attracted to s ...

  4. 2017多校第4场 HDU 6078 Wavel Sequence DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:求两个序列的公共波形子序列的个数. 解法: 类似于最长公共上升子序列,对于每个i,只考虑存 ...

  5. hdu 6078 Wavel Sequence

    题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6078 (2017 Multi-University Training Contest - Team ...

  6. (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][ ...

  7. HDU 6078 Wavel Sequence(dp)

    Description 定义波浪序列:a1 > a2 < a3-,现在给出一个长度为n的序列a和一个长度为m的序列b,求a和b的公共波浪子序列个数 Input 第一行一整数T表示用例组数, ...

  8. hud 6078 Wavel Sequence

    hud 6078 Wavel Sequence 题目大意:给你两个序列a,b,让你找出两个函数 f 和 g 使得 a[f]=b[g],并且a[f1],a[f2],a[f3]--a[fk]满足 序列a1 ...

  9. HDU 1005 Number Sequence

    [题目]                                                   Number Sequence Time Limit: 2000/1000 MS (Jav ...

最新文章

  1. mysql 建立root用户名和密码_MYSQL中5.7.10ROOT密码及创建用户
  2. 权威预测:2018年这十大数字化转型趋势要火!
  3. PAT L3-002. 堆栈
  4. 【音频处理】Adobe Audition 快捷键设置
  5. CSS3与页面布局学习笔记(二)——盒子模型(Box Model)、边距折叠、内联与块标签、CSSReset
  6. python垃圾邮件识别_【Python】垃圾邮件识别
  7. Hibernate实体映射配置1(java@注解方式)
  8. 微信短内容要来了!新功能已开启内测申请
  9. 隐藏多行文本框的滚动条
  10. 求ucinet软件下载资源!!!!
  11. 51单片机蜂鸣器播放音乐C语言程序实例,51单片机 使用蜂鸣器播放简单音乐
  12. hadoop学习之路(5)
  13. OpenGL 亮度调节
  14. 脱口而出的 “ 感谢的语言 ”
  15. 基于Springboot和VUE的聊天项目,仿PC端微信
  16. 计算机考试不在学籍库,有消息!中考报名将由学籍库直接导入,取消学校考试排名........
  17. p0f - 被动探测操作系统工具
  18. 密码锁 Locker
  19. JDBC占位符的使用
  20. (十二)命令模式详解(故事版)- 转

热门文章

  1. 小白友好——C++基于EeayX简单开发的豪华版贪吃蛇[单人模式+双蛇对战+闯关模式(地图跳转)+排行榜+音乐]
  2. 卓训教育:孩子不爱学习怎么办,如何让孩子将动力内化
  3. java 批量设置单元格边框,VC下设置Excel单元格的边框 (转)
  4. U大师U盘启动盘克隆制作工具
  5. 税费计算机,友商税费计算器
  6. 生产环境使用 pt-table-checksum 检查MySQL数据一致性【转】
  7. MacOS 校验iso sha256值、md5值,linux
  8. Linux用到的大数据相关命令
  9. 利用媒体云实现差异化
  10. 商用咖啡机 推荐.html,商用咖啡机推荐,半自动咖啡机如何使用?