[Universal OJ] #311: [UNR #2] Illness from Overwork — Solution
Main Idea
This way of handling it is really neat. According to yyb's blog, this technique is called “divide and conquer on the maximum”.
We can consider designing the state \(dp[i][j]\) as the number of ways for length \(i\) with maximum value not exceeding \(j\). Then we can enumerate a \(k\) as the position of \(j\), and force this \(k\) to be the rightmost \(j\) in this segment. Written out concretely, the transition is:
\[ dp[i][j] = dp[i][j - 1] + \sum_{k = 1}^i dp[k - 1][j] \times dp[i - k][j - 1] \times w^c \]
Note the \(dp[i - k][j - 1]\) on the right side, which guarantees that this position is the rightmost \(j\), so nothing gets counted twice. Here \(c\) is the number of intervals in the current sequence that contain this number; draw a picture, and just compute the difference between the left edges of the rightmost and leftmost intervals.
Code
// UOJ311.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 440, mod = 998244353;
int n, m, wi[MAX_N][MAX_N], dp[MAX_N][MAX_N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &wi[1][i]), wi[0][i] = 1;
for (int j = 2; j <= m; j++)
wi[j][i] = 1LL * wi[j - 1][i] * wi[1][i] % mod;
}
for (int i = 0; i <= n; i++)
dp[0][i] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
dp[i][j] = dp[i][j - 1];
for (int k = 1; k <= i; k++)
{
int tmp = 1LL * dp[k - 1][j] * dp[i - k][j - 1] % mod;
int idx = max(0, min(k, i - m + 1) - max(1, k - m + 1) + 1);
// int idx = min({m, max(0, i - m + 1), k, i - k + 1});
tmp = 1LL * tmp * wi[idx][j] % mod;
dp[i][j] = (0LL + dp[i][j] + tmp) % mod;
}
}
printf("%d\n", dp[n][n]);
return 0;
}