HDOJ 6078-Wavel Sequence
Wavel Sequence
Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 511????Accepted Submission(s): 272題目鏈接:點擊打開鏈接
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(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).
題意:
給出一個有n(<=2000)個數字的序列 a(ai <=2000) 再給出一個有m(m<=2000)個
數字的序列 b(bi<=2000) ,定義波浪序列為:x1<x2>x3<x4……(注意第一次必須
是上升,不能是下降,也就是說第一項必須是波谷,這也可以讓我們明白,相對于
一個數來說,不管大小,它都可以作為 波谷的,因為下一個數一定要大于它)。現
在要求從a中找到序列 f:f1,f2,……fk。相對應的在b中找序列g:g1,g2,……
gk。(k>=1)使得每個a_fi =b_gi,同時滿足a_f1,a_f2,a_f3……a_fk為波浪序列。求
不同的fg映射有多少種選取方式。
a,b中分別從前向后選取k個數字。然后相對應的 a 中選擇的每個位置的數字要和 b?
中選擇的對應位次的數字相同。(當然如果a數組出現過x,而b沒有出現過x,顯然x
不可能被選取),而 f 、g 則是相對應的下標。要滿足選取出來的這個數字序列是一
個波浪序列。顯然波浪序列中的數字分成兩種:波峰和波谷。
總體來說,這個題就是a、b數組之間的匹配問題,同時滿足是一個波浪序列。
分析: 用一個二維數組dp來存儲b數組中每個數字作為波峰和波谷的兩種情況: ?
?????? dp[ ][0]用來表示當前數字為波谷時的情況?
?????? dp[ ][1]用來表示當前數字為波峰時的情況?
?????? 每輪查找都不斷更新dp的值?
再用一個二維數組sum來存儲b數組中每個數字作為波峰和波谷的次數,它的作用
其實是為在b數組中如果遇 到下一個和a[i]相等的數時做統計。
用sum[][0]來存儲該數字在波谷時的總的情況數?
?????? sum[ ][1]用來存儲該數字在波峰時的總的情況數?
其實sum的作用是為后面的查詢做統計(這個代碼是用b數組作統計的)
? ? ? ?下面我們來看代碼實現吧。
#include<bits/stdc++.h> using namespace std;typedef long long LL; const LL MOD = 998244353; const int N = 2e3+5;int b[N], a[N]; LL sum[N][2], dp[N][2];///sum存儲b數組中每位數字兩種情況下的總值;int main() {int T, n, m;scanf("%d", &T);while(T--){memset(sum,0,sizeof(sum));memset(dp,0,sizeof(dp));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;///ans是計算b中所有數字能當波峰和波谷的所有情況,也就是最終的答案for(int i = 1; i <= n; i++)///以a數組當前查詢的點作為波浪結束點{LL cnt0 = 1, cnt1 = 0;///cnt0表示波谷位置,cnt1表示波峰位置for(int j = 1; j <= m; j++){if(a[i] == b[j])///如果在b數組中查詢到有相等的,則說明該點是波浪的結束點,///開始統計它作為波峰和波谷的情況{dp[j][0] = cnt0;///dp[][0]表示b[j]在波谷的情況dp[j][1] = cnt1;///dp[][1]表示b[j]在波峰的情況ans = (ans+cnt1+cnt0)%MOD;///ans表示計算兩種情況的總合}else if(b[j] < a[i])///如果b[j]<a[i],說明b[j]的值可以做a[i]的值得波谷,換句話說,///當在b數組中找到與a[i]值相同的數時,可以統計它相對作為波峰的情況(cnt1 += sum[j][0]) %= MOD;else///同上理解(cnt0 += 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; }
總結
以上是生活随笔為你收集整理的HDOJ 6078-Wavel Sequence的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库初级入门sqlite3版本
- 下一篇: 火锅尝后感,桌面虚拟化服务真的很重要