Lin1991122's blog

By Lin1991122, history, 5 hours ago, In English

CSP-S 打炸了,然后晚上又打炸了,有点破防。

设 $$$f_{i,j}$$$ 表示 $$$k=i$$$,$$$a$$$ 序列中我已经选了前 $$$j$$$ 个。初始 $$$f_{1,0}=0$$$,其余为极大值。每次可以令 $$$k$$$ 加一,于是 $$$f_{i+1,j}=\min(f{i+1,j},f_{i,j})$$$。来考虑同层之间的转移,可以二分出极长合法段,设其长度为 $$$len$$$,只对 $$$f_{i,j+len}$$$ 转移显然是答案不劣的,但这样会导致方案统计少。考虑如下情况,$a$ 序列为 10 1 1,$b$ 序列为 11,我第一次不一定非要取出 $$$len=2$$$ 个才最优,显然也可以只取出 $$$1$$$ 个。因此,为了完成 D2,我们需要对 $$$[j+1,j+len]$$$ 都更新答案。我们可以使用 set 来维护这一过程。

具体地,set 维护当前所有合法转移及其方案数,每个 $$$f_{i,j}$$$ 的贡献在 $$$j+len+1$$$ 处消失,我们可以在那里删去它的贡献。注意到我们要合并 set 中的相同代价的转移的方案数,因此可以使用 mutable 关键字来完成。每次从 set 中取出最小值进行转移。注意,我们不能认为一个转移的方案数在模意义下为 $$$0$$$ 时等价于这个转移失效,应该额外记录,否则你过不了 pretest,应该 WA on 29。

下面这份代码中的注释是不用 set 维护而暴力转移的办法,可以配合理解。

Code

Full text and comments »

  • Vote: I like it
  • -10
  • Vote: I do not like it

By Lin1991122, history, 2 months ago, In English

Ignoring TLE,let's calculate $$$f(s)$$$ at first.I'd like to transform the problem into this:divide the sequence into some segments whose length is more than 2,each segment's cost is the first number times the length -1.For example,2 2 2,it's cost is 4.Set $$$f_{i,j}$$$ means,explore first i elements,the first number of the last segment is j.It's easy to calculate now.

$$$\begin{cases} & f_{i,j}=f_{i-1,j}+j \\ & f_{i,a_{i-1}}=\min(f_{i,a_{i-1}},\min_1^9f_{i-2,j}+a_{i-1}),i\geq4 \end{cases}$$$

Means that,I can append $$$i_{th}$$$ number just in the back of the last segment,or make a new segment with $$$a_{i-1}$$$.

It costs $$$O(n)$$$ for each query,and got TLE.We can use ddp.Use matrix to draw the dp.Especially,the $$$\times$$$ means add.the $$$+$$$ means $$$\min$$$.Let $$$\min_i=\min_1^9 f_{i,j}$$$,we can design the vector as follows:

$$$\begin{bmatrix} f_{i-1,1}\\ f_{i-1,2}\\ f_{i-1,3}\\ f_{i-1,4}\\ f_{i-1,5}\\ f_{i-1,6}\\ f_{i-1,7}\\ f_{i-1,8}\\ f_{i-1,9}\\ \min_{i-1}\\ \min_{i-2} \end{bmatrix}$$$

I'd like to show you $$$i_{th}$$$ matrix with $$$a_{i-1}=2$$$("i" means inf):

$$$\begin{bmatrix} 1& i& i& i &i& i& i& i& i &i &i \\ i& 2& i& i &i& i& i& i& i &i &2 \\ i& i& 3& i &i& i& i& i& i &i &i \\ i& i& i& 4 &i& i& i& i& i &i &i \\ i& i& i& i &5& i& i& i& i &i &i \\ i& i& i& i &i& 6& i& i& i &i &i \\ i& i& i& i &i& i& 7& i& i &i &i \\ i& i& i& i &i& i& i& 8& i &i &i \\ i& i& i& i &i& i& i& i& 9 &i &i \\ 1& 2& 3& 4 &5& 6& 7& 8& 9 &i &2 \\ i& i& i& i &i& i& i& i& i &0 &i \end{bmatrix}$$$

It's easy to use segment tree to solve this,but might TLE.Just use blocks,since the len of each query is const.

Also,I'm working on how to get the inverse of the matrix.Guass might work.

code

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it