HDU6078 Wavel Sequence(动态规划)
来源:互联网 发布:网络 信息 平台 建设 编辑:程序博客网 时间:2024/05/18 01:27
Wavel Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 728 Accepted Submission(s): 377
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...
Picture from Wikimedia Commons
Now given two sequencesa1,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 sequencesf and g he can find. Please write a program to help him figure out the answer.
Picture from Wikimedia Commons
Now given two sequences
Moreover, Little Q is wondering how many such two sequences
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 are2 integers n,m(1≤n,m≤2000) in the first line, denoting the length of a and b .
In the next line, there aren integers a1,a2,...,an(1≤ai≤2000) , denoting the sequence a .
Then in the next line, there arem integers b1,b2,...,bm(1≤bi≤2000) , denoting the sequence b .
In each test case, there are
In the next line, there are
Then in the next line, there are
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
13 51 5 34 1 1 5 3
Sample Output
10Hint(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
Recommend
liuyiding
题意:给出两个数列,找到两个数列中成波浪状的公共子数列的个数
首先可以找到一个很明显的递推式,dp[i][j][0]表示a[i]和b[j]结尾且最后一个为波谷,dp[i][j][1]表示a[i]和b[j]结尾而且最后一个为波峰
如果a[i]!=b[j] dp[i][j][0]=0且dp[i][j][1]等于0
否则,dp[i][j][0]等于所有p<i和q<j的dp[p][q][1]的情况加起来,dp[i][j][1]亦然
所以我们用两个数组记录,sum[i][j][0]表示以b[j]结尾,所有k<=i的dp[k][j][0]的情况加起来,这样就可以优化到n^2的复杂度了。
#include<bits/stdc++.h>#define fi first#define se second#define pb push_back#define CLR(A, X) memset(A, X, sizeof(A))using namespace std;typedef long long LL;typedef pair<int, int> PII;const double eps = 1e-10;int dcmp(double x){if(fabs(x)<eps) return 0; return x<0?-1:1;}const LL INF = 0x3f3f3f3f;const LL MOD = 998244353;const int N = 2e3+5;int b[N], a[N];LL sum[N][2], dp[N][2];int main() {// freopen("1001.txt", "r", stdin); int T, n, m; scanf("%d", &T); while(T--) { CLR(sum, 0); CLR(dp, 0); scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= m; i++) scanf("%d", &b[i]); LL ans = 0; for(int i = 1; i <= n; i++) { LL cnt1 = 1, cnt0 = 0; for(int j = 1; j <= m; j++) { dp[j][0] = dp[j][1] = 0; if(a[i] == b[j]) { dp[j][0] = cnt1; dp[j][1] = cnt0; ans = (ans+cnt1+cnt0)%MOD; } else if(b[j] < a[i]) (cnt0 += sum[j][0]) %= MOD; else (cnt1 += sum[j][1]) %= MOD; } for(int j = 1; j <= m; j++) { if(b[j] == a[i]) { (sum[j][0] += dp[j][0]) %= MOD; (sum[j][1] += dp[j][1]) %= MOD; } } } printf("%lld\n", ans); } return 0;}
阅读全文
0 0
- HDU6078 Wavel Sequence(动态规划)
- HDU6078-Wavel Sequence
- HDU 6078 Wavel Sequence【动态规划】
- hdu6078 Wavel Sequence 2017多校第四场1012 dp
- 2017多校训练Contest4: 1012 Wavel Sequence hdu6078
- hdu6078 Wavel Sequence(分析优化递推过程,好题)
- hdu6078 wave sequence dp
- HDU 6078 Wavel Sequence
- HDU 6078 Wavel Sequence
- HDU_6078 Wavel Sequence
- HDU 6078Wavel Sequence
- HDOJ 6078-Wavel Sequence
- HDU 6078 Wavel Sequence
- hud 6078 Wavel Sequence
- HDU 6078 Wavel Sequence
- HDU_6078 Wavel Sequence 【DP】
- HDU 6078 Wavel Sequence
- Wavel Sequence HDU
- hibernate的sessionFactory和session
- 树形结构的数据库表Schema设计
- HDU 3652 B-number(数位DP)
- windows下 安装 rabbitMQ 及操作常用命令
- Spring Cloud(七):链路追踪Sleuth-Zipkin与Mysql数据的持久化
- HDU6078 Wavel Sequence(动态规划)
- HDU 3613 吉哥系列故事――完美队形II(Manacher模板稍加改动)
- 目录树结构的数据库设计思考
- Spring Boot项目中自定义注解的使用
- 装饰类PrintStream实例浅析
- 图像处理之------匹配
- linux压缩命令学习小结
- Spring整合Quartz2.1.1(执行中,得到application上下文,执行bean方法)
- Hive(十七)--排序