HDU 6078 Wavel Sequence【动态规划】

题目来戳呀

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 = bgi ,同时 afi 为波浪序列,求满足此映射关系的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:攒了一堆博客没写呜呜呜

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值