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
- XX consists of gems with non-decreasing beautifulness,
- yy is the only perfect gem. (A perfect gem is a gem whose beautifulness equals to 1000010000)
- 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 1≤n≤105). 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 0≤ai≤104,1≤number of perfectgems≤10).
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