SCU - 4441 环形dp+树状数组优化

这是一篇关于使用环形动态规划和树状数组优化算法解决项链问题的博客。题目要求在保持宝石相对顺序不变的情况下,通过移除部分宝石形成一个美丽的项链,其中包含一个完美宝石(价值10000)。文章详细解释了思路,即通过复制数组并利用树状数组求解非递增和非递减序列的最大和,最终在O(n log n)的时间复杂度内找到最大剩余价值。并给出了超时代码作为对比。
摘要由CSDN通过智能技术生成

SCU - 4441 环形dp+树状数组优化

Necklace

frog has nn gems arranged in a cycle, whose beautifulness are a1,a2,…,ana1,a2,…,an. She would like to remove some gems to make them into a beautiful necklace without changing their relative order.

Note that a beautiful necklace can be divided into 3 consecutive parts X,y,Z where

  1. XX consists of gems with non-decreasing beautifulness,
  2. yy is the only perfect gem. (A perfect gem is a gem whose beautifulness equals to 1000010000)
  3. ZZ consists of gems with non-increasing beautifulness.

Find out the maximum total beautifulness of the remaining gems.

Input

The input consists of multiple tests. For each test:

The first line contains 1 integer n ( 1 ≤ n ≤ 1 0 5 1≤n≤10^5 1n105). The second line contains nnintegers a1,a2,…,an a1,a2,…,an. ( 0 ≤ a i ≤ 1 0 4 , 1 ≤ n u m b e r   o f   p e r f e c t g e m s ≤ 10 0≤ai≤10^4,1≤number~of~perfect gems≤10 0ai104,1number of perfectgems10).

Output

For each test, write 11 integer which denotes the maximum total remaining beautifulness.

Sample Input

    6
    10000 3 2 4 2 3
    2
    10000 10000

Sample Output

    10010
    10000

题意:

N个数构成一个环,现在可以删除一些数,使得这个环可以分成连续的三部分:

X部分:所有数不降

Y部分:仅含一个值为10000的数

Z部分:所有数不增

思路:

​ 大体思路就是枚举每个10000的点,再枚举中间的每个端点,求10000右边的非递增序列最大和,求10000左边的非递减序列最大和。但是我们可以把这个数组复制成原来的两倍,那么 i d id id 的复制品 i d + n id+n id+n 之间的就是环的其他部分,我们可以用树状数组求每个端点从左边开始的非递增序列最大和 以及 每个端点从右边开始向左边的非递增序列最大和,时间复杂度为O(nllogn)。最后O(n)枚举分裂点求最大即可

AC代码:

#include<bits/stdc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值