HDU 6078 Wavel Sequence

來源:互聯網
上載者:User

類似於最長公用上升子序列,對於每個i,只考慮存在j使得a[i]==b[j]的情況。
dp[i][j][0]表示以a[i]和b[j]為公用序列結尾且為波穀的情況總和。
dp[i][j][1]則表示波峰的情況總和。
S[i][j][0]表示sum(dp[k][j][0] | 1<=k<=j-1)。
S[i][j][1]則表示sum(dp[k][j][1] | 1<=k<=j-1)。
那麼對於每個a[i],只有存在j使得b[j]==a[i]時,dp[i][j][0]等於sum(S[i-1][k][1] | 1<=k<=j-1&&b[k]>a[i])+1,dp[i][j][1]等於sum(S[i-1][k][0] | 1<=k<=j-1&&b[k]<=a[i]-1).
可以用滾動數組最佳化掉一維空間。

代碼:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.