9
19
2016
5

codeforces 720F

假如我死在了这道题上,给我烧点纸钱。

英文题解细节实现没有提及,但细节实现非常之麻烦。这里主要是翻译解释并加了细节的如何实现。

记p=max(k-n,0)+x,q=k-p,x是一个随便选的0到n-1的数。

选的区间由两部分构成,第一部分是为了使总和尽可能大的p个区间,显然要选前p大的区间,第二部分是为了覆盖第一部分没有覆盖的位置而选的q个区间。非常显然q≤n所以x取值范围是1到n。

我们维护出第一部分没有覆盖的点{i1,i2,...is},考虑怎么选才能让第二部分的贡献尽可能大。

易证第二部分选的区间没有重叠部分且一定覆盖某个i,否则答案一定不优。

记q个区间是[l1,r1],[l2,r2]...[lq,rq],很显然sum(r)取得越大越好,sum(l)取得越小越好。易证rj和lj+1一定会落在某个区间[ij,ij+1]内。所以sum(l1)的最小值就是min(sum(j)(j<i1)),sum(rq)同理,对于中间的rj和lj+1,易得它们的最大贡献是-区间[ij+1,ij+1]内的最小子序列和。所以第二部分对答案的最大贡献就是min(sum(j)(j<i1))+max(sum(j)(j>=iq))-前(q-1)小的区间[ij+1,ij+1]内的最小子序列和。

然后考虑第一部分的贡献,可以用树状数组求>=mid的子序列数量,所以我们可以二分第p大的子序列个数,可以用类似的方法求出他们的和以及对于某个r来说最小的合法的l。

然后可以用堆维护第k大的子序列,维护对于以某个位置结尾的子序列来说,比当前值小的子序列中最大的子序列。每次从第k大的序列转移到第k+1大的序列就从堆顶拖一个子序列出来,然后对于它的结尾找下一个子序列到堆里。

整理一下。我们先做x=0的情况,用树状数组求出前p大的区间和的和记为ans1,然后在线段树上覆盖这些区间,从而找出没有被覆盖的点。第一二项直接查,第三项用线段树维护出区间最小子序列和,然后用一个支持删除,插入,求前k小项的和的数据结构(我选择splay)维护前(q-1)小的区间[ij+1,ij+1]内的最小子序列和。得出第二部分的贡献ans2。总贡献就是ans1+ans2。

然后将x从1枚举到n-1,每次x++的时候,找出新加入的区间,ans1加上它的值,暴力找出所有它覆盖的点,维护那个数据结构从而维护ans2,就可以在均摊O(nlog)时间内得出min(ans1+ans2)。

明天再写。

UPDATE 9.23:一位红名大爷证明了子序列值相等的话无论按照什么顺序都一定可以得出最优解。并且之前的写法是有问题的。不能直接维护那个堆,算了写完之后看代码再说吧。

UPDATE9.27:终于胖出来了,弄了半天WA27的原因是checksum里少加了一个else导致某个子区间被加到堆里不止一次然后就狗带了。智商低出新高度

PS:http://pan.baidu.com/s/1o8TMZxS RCC2016的数据和std,省的后来的人去胖俄文了

 

Category: codeforces | Tags: 树状数组 线段树 codeforces Splay | Read Count: 2637
Avatar_small
qiancl 说:
2016年9月20日 09:17

%%%%%%%%%%%%%%%%%%%%%%%%%%%

Avatar_small
WasteRice 说:
2016年9月23日 09:45

If some pair of subsegments have same sum, it doesn't matter, which of them you will take in "k - n" (k > n) set. Let's see, why:
Let's sort all subsegment by decreasing of their sum (we don't care now about subsegments with equal sum). Suppose, we didn't take first k - n segments, and x (x ≤ k - n) is 1-based index of first subsegment which is not in optimal answer. Obviously, there are k - x + 1 segments in answer with indexes bigger than x. We can see that k - x + 1 ≥ k - (k - n) + 1 = n + 1. We can always choose one subsegment from  ≥ n + 1 segments so that other  ≥ n segments cover whole array (suppose we can't: every segment covers some index in array that is not covered by any of other segments, but we have n + 1 segments and only n values in array). Let's delete this segment from our answer, and add segment with index x to answer. We didn't lose property that our set of segments cover whole array, and we didn't decrease sum of our segments: segment with index x had greater or equal sum than segment that we deleted from our set.
So, if there is an optimal answer, we showed that there is an optimal answer with any k - n subsegments with maximal sum, without worrying about subsegments with equal sum.

Avatar_small
WasteRice 说:
2016年9月23日 09:46

You can do that faster than in O(n2log) if you, for example, sort segments by tuple <sum[l..r], r, l>. Suppose there are x (x < k) segments with sum  > y for some y, and there are  > k segments with sum  ≥ y. Indeed, there can be a lot (about O(n2)) subsegments with sum equal to y, so it is not easy to enumerate n segments that we need. We need just to skip first k - n - x segments with sum y, and then you can take segments with sum y one by one, because we will do that only n times.
In order to skip first segments we can use some data structure that can add new integer into structure and answer 2 types of questions: how many integers less than given are there in the structure, and what is i-th integer in structure. It can be done by fenwick tree (built on sorted prefix sums), or by advanced C++ STL data structures, like this.
When we have such data structure, when we fixed r bound of current subsegment, we need to ask how many integers equal to s[r] - y are there in our structure (because s[r] - s[l - 1] should be equal to y). If we can add them all to "k-n" set, just skip all segments with this r. Otherwise, you need to skip first k - n - currentSzeOfKnSet, and enumerate next few segments. Here you will need to know i-th integer in the structure.

Avatar_small
qiancl 说:
2016年9月27日 12:31

楼上的就是那个红名大爷吗?

Avatar_small
Tripura Board Questi 说:
2022年8月19日 14:47

Tripura Board Model Paper 2023 Class 1 Pdf Download with Answers for Bengali Medium, English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. Tripura Board Question Paper Class 1 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com