[BZOJ5427]最长上升子序列/[BZOJ4282]慎二的随机数列

题目大意:

给你一个长度为\(n(n\le10^5)\)的整数序列,其中有一些数已经模糊不清了,现在请你任意确定这些整数的值,使得最长上升子序列最长。求最长长度。

思路:

一定存在一种最优方案使得不确定的都选上(考虑新选上一个不确定的数,最多会使一个已确定的数失效),因此令\(a_i=a_i-cnt\)(\(cnt\)为之前不确定的数的个数),求LIS后加上\(cnt\)即可。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
inline char getupper() {
register char ch;
while(!isupper(ch=getchar()));
return ch;
}
const int N=1e5+1;
int a[N],tmp[N];
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=tmp[0];p+=lowbit(p)) {
val[p]=std::max(val[p],x);
}
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) {
ret=std::max(ret,val[p]);
}
return ret;
}
};
FenwickTree bit;
int main() {
const int n=getint();
int cnt=0;
for(register int i=1;i<=n;i++) {
if(getupper()=='K') {
a[i]=tmp[i-cnt]=getint()-cnt;
} else {
a[i]=INT_MAX;
cnt++;
}
}
std::sort(&tmp[1],&tmp[n-cnt]+1);
tmp[0]=std::unique(&tmp[1],&tmp[n-cnt]+1)-&tmp[1];
for(register int i=1;i<=n;i++) {
if(a[i]==INT_MAX) continue;
a[i]=std::lower_bound(&tmp[1],&tmp[tmp[0]]+1,a[i])-tmp;
bit.modify(a[i],bit.query(a[i]-1)+1);
}
printf("%d\n",bit.query(tmp[0])+cnt);
return 0;
}

最新文章

  1. Chrome 开发工具之Timeline
  2. demo和实际项目的距离
  3. php示例代码之使用mysqli对象
  4. Pascal’s Triangle
  5. strange error encountered today in ROS
  6. 第46套题【STL】【贪心】【递推】【BFS 图】
  7. jsp页面变量作用域问题
  8. [转]支持向量机SVM总结
  9. OutputStream类详解
  10. DataBase MongoDB基础知识记录
  11. vue修改端口号
  12. Handler实现与机制 &amp;&amp; Blocking Queue &amp;&amp; IdleHandler使用
  13. 转载:分布式文件系统 - FastDFS 在 CentOS 下配置安装部署(2)
  14. 混合型log,info按大小分,error按日期
  15. fio是如何运行的?
  16. python学习之老男孩python全栈第九期_day023知识点总结——类和对象命名空间、组合
  17. (转)全局变量、extern/static/const区别与联系
  18. centos下利用mail命令进行邮件发送
  19. Fiddler给网站“优化”
  20. Python3 标准库:calendar,time

热门文章

  1. 【vim】保存文件并退出 :w=:wq
  2. C语言函数调用栈(三)
  3. OpenWrt启动过程分析+添加自启动脚本【转】
  4. Vistual Studio Community 2017 账号的许可证过期,公安网激活方法
  5. java项目中oracle配置说明
  6. 转载:《理解RESTful架构》 阮一峰
  7. 16-client、offset、scroll系列
  8. vue系列之项目结构
  9. node path.resolve()
  10. java多线程快速入门(五)