HDU6078 Wavel Sequence(动态规划)

本文介绍了一种算法,用于解决寻找两个数列中成波浪状的公共子数列的问题,并提供了一个优化的实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 sequences  a1,a2,...,an  and  b1,b2,...,bm , Little Q wants to find two sequences  f1,f2,...,fk(1fin,fi<fi+1)  and  g1,g2,...,gk(1gim,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(1T15) , denoting the number of test cases.

In each test case, there are  2  integers  n,m(1n,m2000)  in the first line, denoting the length of  a  and  b .

In the next line, there are  n  integers  a1,a2,...,an(1ai2000) , denoting the sequence  a .

Then in the next line, there are  m  integers  b1,b2,...,bm(1bi2000) , 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
 

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值