Sth about Educational DP Contest

Contest Website : atcoder.jp/contests/dp

\[\begin{array}{c|C|c|c} TaskNum & TaskName & Status & Algorithm \\ \hline A & Frog 1 & \color{green}{AC} & \text{简单线性DP} \\ \hline B & Frog 2 & \color{green}{AC} & \text{简单线性DP,TaskA加强版} \\ \hline C & Vacation & \color{green}{AC} & \text{简单线性DP} \\ \hline D & Knapsack 1 & \color{green}{AC} & \text{OI背包} \\ \hline E & Knapsack 2 & \color{yellow}{WA} & \text{01背包重大价小} \\ \hline F & LCS & \color{green}{AC} & \text{最长公共子序列} \\ \hline G & Longest Path & \color{green}{AC} & \text{DAG上DP} \\ \hline H & Grid 1 & \color{green}{AC} & \text{矩阵DP} \\ \hline I & Coins & \color{green}{AC} & \text{概率DP} \\ \hline J & Sushi & \color{green}{AC} & \text{期望DP} \\ \hline K & Stones & \color{green}{AC} & \text{博弈论} \\ \hline L & Deque & \color{green}{AC} & \text{区间DP} \\ \hline M & Candies & \color{green}{AC} & \text{前缀和优化线性DP} \\ \hline N & Slimes & \color{green}{AC} & \text{区间DP} \\ \hline O & Matching & \color{green}{AC} & \text{状压DP} \\ \hline P & Independent Set & \color{green}{AC} & \text{树形DP} \\ \hline Q & Flowers & & \\ \hline R & Walk & & \\ \hline S & Digit Sum & & \\ \hline T & Permutation & & \\ \hline U & Grouping & & \\ \hline V & Subtree & & \\ \hline W & Intervals & & \\ \hline X & Tower & & \\ \hline Y & Grid 2 & & \\ \hline Z & Frog 3 & & \\ \end{array} \]

A. Frog 1

We define \(f_i\) as the minimum cost for the frog to jump from the 1st stone to the \(i\)-th stone.
And we know that the frog can only jump from the \((i-1)\)-th stone or the \((i-2)\)th stone to the \(i\)-th stone. Thus, we can know the equation. It is:

\[f_i=\operatorname{min}\{f_{i-1}+\left\vert h_i-h_{i-1} \right\vert ,f_{i-2}+\left\vert h_i-h_{i-2} \right\vert \} \]

\(O(n)\) ~

B. Frog 2

\[f_{i+j}=\operatorname{min}\{f_i+\left\vert h_i-h_{i+j}\ \right\vert\},1\le j\le k \]

Why don't I use \(f_{i-j}\) to transfer to \(f_i\)? That's because I don't want to check if \(i-j < 0\). I think this is an unimportant skill 😛
\(O(nk)\) 😃

C. Vacation

Easy~
We define \(f_{i,j}\) is the maximum points of happiness at Day i, and we choose activity j at Day i. We cannot choose activity j at Day i+1.
So, \(f_{i,j}\) can be transfered from \(f_{i-1,k}\, ,k\neq j\).

\[f_{i,1} = \operatorname{max}\{f_{i-1,2},f_{i-1,3}\} + a_i \]

\[f_{i,2} = \operatorname{max}\{f_{i-1,1},f_{i-1,3}\} + b_i \]

\[f_{i,3} = \operatorname{max}\{f_{i-1,1},f_{i-1,2}\} + c_i \]

The answer is \(\operatorname{max}\{f_{n,1},f_{n,2},f_{n,3}\}\).
\(O(n)\) ~

D. Knapsack 1

01 backpack.
Because I cannot explain it in English, so I will only write the equation.
\(i\) is the i-th item, \(v\) refers to the remaining capacity.

\[f_{v}=\operatorname{max}\{f_v,f_{v-w_i}+c_i\} \]

\(O(nw)\)

F. LCS

嘤语不会用了QAQ
定义 \(f_{i,j}\)\(s\) 串前 \(i\) 个字符和 \(t\) 串前 \(j\) 个字符的LCS。

\[f_{i,j}=\begin{cases} f_{i-1,j-1}+1, & s_i=t_j \\ \operatorname{max}\{f_{i-1,j},f_{i,j-1}\}, &\text{otherwise} \end{cases} \]

\(O(n^2)\)

G. Longest Path

We have two methods to solve this task.
First, we use Topological sorting. Then, we can traverse the topological order from back to front.
Second, we can use the memorizing search method.

\[f_{i}=\operatorname{max}\{f_{j}+1\} \]

\(O(n+m)\)

H. Grid 1

Matrix DP.
We can walk to the right and the bottom point.
So, we can walk from the left and the top point.

\[f_{i,j}=f_{i-1,j}+f{i,j-1} \]

\(O(HW)\)

I. Coins

Probability DP.
We define f[i][j] is the probability of \(j\) out of the first \(i\) coins turned heads.
So, if we need \(j\) coins turns heads, we have \(2\) options.

  1. There are \(j\) out of the first \(i - 1\) coins turned heads and the i-th coin flip to the back.
  2. There are \(j - 1\) out of the first \(i - 1\) coins turned heads and the i-th coin turn to the front.

\[f_{i,j}=f_{i-1,j} * (1-p_i) + f_{i-1,j-1}*p_i \]

\(O(n^2)\)

J. Sushi

求期望。
\(f_{i,j,k}\) 为还剩 \(i\) 个盘子有一个寿司,\(j\) 个盘子有两个寿司,\(k\) 个盘子有三个寿司时的期望值。
方程不会写。
\(O(n^3)\)

K. Stones

Game theory.
If there left \(k\) stones left and this state can win, then there must a state of \(f\) that \(k-a_i=f\) and this state must lose.

\[f_{i} = 1, f_{i - a_j} = 0\; and\;1 \le j \le n \]

L. Deque

The first type of Range DP.
We define \(f_{i,j}\) as the maximum value the first people can get in the range \([i,j]\).
So, \(f_{i,j}\) can be translated from \(f_{i+1,j}\) and \(f_{i,j-1}\).

\[f_{i,j}=\sum_{i-1}^{j} a_i - \operatorname{min}\{f_{i+1,j},f_{i,j-1}\} \]

\(O(n^2)\)

M. Candies

Prefix Sum Optimization.
First, we all know that

\[f_{i,j}=\sum_{k=j-a_i}^{j} f_{i,k} \]

But the time complexity of this algorithm is \(O(nk^2)\), So we cannot pass this task with this algo.
So we need Prefix Sum Optimization. We define \(pre_{m}\) as \(\sum_{k=1}^{m}f_{i,k}\).

\[f_{i,j}=pre_j-pre_{j-a_i-1} \]

posted @ 2021-07-04 16:53  1358id  阅读(25)  评论(0编辑  收藏  举报