SCU - 4441 Necklace(树状数组求最长上升子数列)
Necklace
frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). 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
- \(X\) consists of gems with non-decreasing beautifulness,
- \(y\) is the only perfect gem. (A perfect gem is a gem whose beautifulness equals to \(10000\))
- \(Z\) 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 \leq n \leq 10^5\)). The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\). (\(0 \leq a_i \leq 10^4\), \(1 \leq \textrm{number of perfect gems} \leq 10\)).
Output
For each test, write \(1\) integer which denotes the maximum total remaining beautifulness.
Sample Input
6
10000 3 2 4 2 3
2
10000 10000
Sample Output
10010
10000
#include <bits/stdc++.h>
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5+;
int C[N];
int dp1[N],dp2[N],b[N],a[*N];
int n,m,k;
int low_bit(int x)
{
return x&(-x);
}
void updata(int pos,int val)
{
for(int i=pos;i<=;i+=low_bit(i))
C[i]=max(C[i],val);
}
int get_max(int pos)
{
int ans=;
for(int i=pos;i>;i-=low_bit(i))
ans=max(ans,C[i]);
return ans;
} int solve(int id)
{
met(C,);
int cnt=;
for(int i=id+;i<id+n;i++)
{
if(a[i]!=)
b[cnt++]=a[i];
}
dp1[]=b[];
updata(-b[],b[]);
for(int i=;i<cnt;i++)
{
dp1[i]=get_max(-b[i])+b[i];
updata(-b[i],dp1[i]);
}
met(C,);
dp2[cnt-]=b[cnt-];
updata(-b[cnt-],b[cnt-]);
for(int i=cnt-;i>=;i--)
{
dp2[i]=get_max(-b[i])+b[i];
updata(-b[i],dp2[i]);
}
int ans=;
for(int i=;i<cnt;i++)
dp1[i]=max(dp1[i],dp1[i-]);
for(int i=cnt-;i>;i--)
dp2[i]=max(dp2[i],dp2[i+]);
dp2[cnt]=;
for(int i=;i<cnt;i++)
ans=max(ans,dp1[i]+dp2[i+]);
ans=max(ans,dp2[]);
return ans+;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
a[i+n]=a[i];
}
int ans=;
for(int i=;i<n;i++)
{
if(a[i]==)
{
ans=max(ans,solve(i));
}
}
printf("%d\n",ans);
}
return ;
}
SCU - 4441 Necklace(树状数组求最长上升子数列)的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
随机推荐
- 【设计模式】 模式PK:包装模式群PK
1.概述 我们讲了这么多的设计模式,大家有没有发觉在很多的模式中有些角色是不干活的?它们只是充当黔首作用,你有问题,找我,但我不处理,我让其他人处理.最典型的就是代理模式了,代理角色接收请求然后传递到 ...
- 基于springmvc静态文件资源配置问题
1.在这里只教大家一种非常实用的 比较简单的一种: 如果两种都配置记得注释掉这种:
- linux启动过程——(三)
- 【目录】Python自动化运维
目录:Python自动化运维笔记 Python自动化运维 - day2 - 数据类型 Python自动化运维 - day3 - 函数part1 Python自动化运维 - day4 - 函数Part2 ...
- Pythone3 sys模块
1.sys.argv 可以实现从程序外部向程序传递参数2.sys.exit() 程序中间退出,exit(0)正常退出,其他为异常退出3.sys.getdefaultencoding() 获取系统编码方 ...
- python基础===isinstance() 函数,判断一个对象是否是一个已知的类型
isinstance(object, classinfo) object -- 实例对象. classinfo -- 可以是直接或间接类名.基本类型或者有它们组成的元组. >>>a ...
- 安全测试===sqlmap(零)转载
本文转自:https://blog.werner.wiki/sqlmap-study-notes-0/ 感谢作者的整理,如有侵权,立删 零.前言 这篇文章是我学习Sqlmap的用法时做的笔记,记录了S ...
- ftrace 的使用【转】
转自:http://blog.csdn.net/wang6077160/article/details/7814279 ftrace 的使用 ftrace 在内核态工作,用户通过 debugfs 接口 ...
- 图论-最近公共祖先-离线Tarjan算法
有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...
- C json实战引擎 二 , 实现构造部分
引言 这篇博文和前一篇 C json实战引擎一,实现解析部分设计是相同的,都是采用递归下降分析. 这里扯一点 假如你是学生 推荐一本书 给 大家 自制编程语言 http://baike.baidu.c ...