日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 6078 Wavel Sequence (dp)

發布時間:2024/3/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。