The k-largest sum of subsequence

题目描述

给定一个数列 a1,a2,⋯,an,对每个二元组 (l,r),1≤l≤r≤n,按以下方法定义一个偏序关系.

记 S(l,r)= \sum_{i=l}^r a_i

对于 (l1,r1),(l2,r2)

  • 若 S(l1,r1)<S(l2,r2),则 (l1,r1)<(l2,r2)
  • 若 S(l1,r1)=S(l2,r2), 且 l1>l2,则 (l1,r1)<(l2,r2)
  • 若 S(l1,r1)=S(l2,r2),且 l1=l2​​, 且 r1>r2, 则 (l1,r1)<(l2,r2)

把每个二元组 (l,r),1≤l≤r≤n,按以上定义从小到大排序, 询问其中第  大的二元组 (从 1 开始数, 第 1 大的就是最大的).

5≤n≤10^5

1≤k≤10

−10^5≤ai≤10^5

输入格式

第一行读入 n,k

第二行读入 n 个整数表示 ai

样例

样例输入

5 10

0 1 0 1 0

样例输出

3 5

First, we calculate the prefix sum S[i]. And We use i as the rightmost endpoint of the subsequence and make decision on the leftmost endpoint j(1<=j<i). If S[j] is the p-th smallest of S[1...i-1], then S[i]-S[j] should be the p-th largest of S[i]-S[1...i-1]. The only thing we need to do is to find out the former k smallest S[j] and keep records of them including three values--- S[i]-S[j], i and j. Organize them as a class and sort them according to the rule stated in the problem. And find out the k-th largest subsequece.

And now we only have one more problem to tackle. How cdo we find out the former k smallest S[j] for each rightmost endpoint i in constant amount of time, so that we can find out all the former k smallest S[j] for all i in O(n)?

Suppose we heve just discussed i as the rightmost endpoint and have successfully found out the former k smallest S[j]. Now we dive into the situation---i+1. We have a new element to consider, namely S[i+1]. We have to compare S[i+1] with each of the former k smallest S[j], and replace one of them as the new small S[j'] if possible.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值