思路:

对于每个i,分别求1~i和i+1~N两部分的最长下降子序列“拼”起来,最终取最大长度即可。学习了如何使用BIT把LIS问题O(N2)算法优化为O(Nlog(N))的算法。

https://www.cnblogs.com/zsyacm666666/p/5703745.html

此外,还有基于二分查找的O(Nlog(N))算法。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
int bit[MAXN], a[MAXN], d1[MAXN], d2[MAXN], n;
int lowbit(int x) { return x & -x; }
void update(int i, int x)
{
while (i <= MAXN) { bit[i] = max(x, bit[i]); i += lowbit(i); }
}
int query(int i)
{
int ans = ;
while (i) { ans = max(ans, bit[i]); i -= lowbit(i); }
return ans;
}
int main()
{
cin >> n;
for (int i = ; i <= n; i++) cin >> a[i];
for (int i = ; i <= n; i++)
{
int tmp = - a[i];
d1[i] = query(tmp - ) + ;
update(tmp, d1[i]);
}
memset(bit, , sizeof bit);
for (int i = n; i >= ; i--)
{
d2[i] = query(a[i] - ) + ;
update(a[i], d2[i]);
}
for (int i = ; i <= n; i++) d1[i] = max(d1[i], d1[i - ]);
for (int i = n; i >= ; i--) d2[i] = max(d2[i], d2[i + ]);
int maxn = ;
for (int i = ; i <= n; i++) maxn = max(maxn, d1[i] + d2[i + ]);
cout << maxn << endl;
return ;
}

最新文章

  1. doT js 模板引擎【初探】要优雅不要污
  2. Oracle 存储过程学习
  3. openerp学习笔记 domain 增加扩展支持,例如支持 &lt;field name=&quot;domain&quot;&gt;[(&#39;type&#39;,&#39;=&#39;,&#39;get_user_ht_type()&#39;)]&lt;/field&gt;
  4. Css3 display用法
  5. git教程--git版本库的使用
  6. js replace(a,b)之替换字符串中所有指定字符的方法
  7. nginx 高可用
  8. 验证码 jsp
  9. IT连创业系列:产品设计之答题模块
  10. Unity NPOI 无法读取xlsx
  11. cpu iowait高排查的case
  12. pymongo基础
  13. [MongoDB]Mongo基本使用
  14. .gitignore无效的原因
  15. Ubuntu18.04下的模拟神器RetroArch
  16. 为什么要使用mybaits
  17. Apache的ProxyPass简单使用
  18. linux 的计划任务 定时任务
  19. Python爬虫:如何爬取分页数据?
  20. Spring4.x所有Maven依赖

热门文章

  1. IE兼容模式
  2. spin_lock、spin_lock_irq、spin_lock_irqsave区别
  3. “There&#39;s no Qt version assigned to this project for platform ” - visual studio plugin for Qt
  4. EOJ Monthly 2018.4 (E.小迷妹在哪儿(贪心&amp;排序&amp;背包)
  5. Balancing Act(树的重心)
  6. 分区时&quot;磁盘上没有足够的空间完成此操作&quot;的解决方法
  7. 1004 n^n的末位数字
  8. 用C++调用C的库函数(转载)
  9. error: expected ‘)’ before ‘PRId64’(转载)
  10. HDU5213(容斥定理+莫队算法)