[TJOI2013]最长上升子序列

题目大意:

给定一个序列,初始为空。将\(1\sim n(n\le10^5)\)的数字插入到序列中,每次将一个数字插入到一个特定的位置。每插入一个数字后输出LIS长度。

思路:

首先用线段树(或rope)求出最终状态的序列,然后就变成了普通的LIS问题。

源代码:

线段树:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {register char ch;while(!isdigit(ch=getchar()));register int x=ch^'0';while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');return x;
}
const int N=1e5+1;
int n,p[N];
class SegmentTree {#define _left <<1#define _right <<1|1#define mid ((b+e)>>1)private:int val[N<<2];void push_up(const int &p) {val[p]=val[p _left]+val[p _right];}public:int find(const int &p,const int &b,const int &e,const int &x) {if(b==e) return x==b-val[p]?b:0;if(mid-val[p _left]<=x) {const int ret=find(p _right,mid+1,e,x+val[p _left]);if(ret) return ret;}return find(p _left,b,mid,x);}void add(const int &p,const int &b,const int &e,const int &x) {val[p]++;if(b==e) return;if(x<=mid) add(p _left,b,mid,x);if(x>mid) add(p _right,mid+1,e,x);push_up(p);}#undef _left#undef _right#undef mid
};
SegmentTree sgt;
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<=n;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() {n=getint();for(register int i=1;i<=n;i++) {p[i]=getint();}for(register int i=n;i>=1;i--) {p[i]=sgt.find(1,1,n,p[i])+1;sgt.add(1,1,n,p[i]);}int ans=0;for(register int i=1;i<=n;i++) {const int tmp=bit.query(p[i])+1;bit.modify(p[i],tmp);ans=std::max(ans,tmp);printf("%d\n",ans);}return 0;
}

rope

#include<cstdio>
#include<cctype>
#include<ext/rope>
inline int getint() {register char ch;while(!isdigit(ch=getchar()));register int x=ch^'0';while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');return x;
}
const int N=1e5+1;
int n,ans[N];
__gnu_cxx::rope<int> s;
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<=n;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() {n=getint();for(register int i=1;i<=n;i++) {s.insert(getint(),i);}for(register int i=0;i<n;i++) {ans[s[i]]=bit.query(s[i])+1;bit.modify(s[i],ans[s[i]]);}for(register int i=1;i<=n;i++) {ans[i]=std::max(ans[i-1],ans[i]);printf("%d\n",ans[i]);}return 0;
}

转载于:https://www.cnblogs.com/skylee03/p/9925499.html

[TJOI2013]最长上升子序列相关推荐

  1. BZOJ 3173: [Tjoi2013]最长上升子序列

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1524  Solved: 797 [Submit][ ...

  2. BZOJ3173 [TJOI2013]最长上升子序列

    题面: 3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2108  Solved: 1067 [Sub ...

  3. [BZOJ]3173: [Tjoi2013]最长上升子序列

    题解:   考虑按照元素升序加入  所以对位置在其后的元素LIS无影响 然后从前面位置的最大值转移过来就行 ,,,,平衡树无脑模拟 #include <algorithm> #includ ...

  4. P4309 [TJOI2013]最长上升子序列 平衡树 + dp

    传送门 文章目录 题意: 思路: 题意: 思路: 注意到一个很关键的条件,每次插入iii,而iii是递增的,也就是说插入iii之后只会从前面的最大值转移过来,所以我们现在只需要维护插入操作即可,这个显 ...

  5. [BZOJ3173][Tjoi2013]最长上升子序列

    [BZOJ3173][Tjoi2013]最长上升子序列 试题描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上 ...

  6. bzoj 3173: [Tjoi2013]最长上升子序列(离线二分+树状数组)

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2051  Solved: 1041 [Submit] ...

  7. BZOJ3173:[TJOI2013]最长上升子序列(Splay)

    Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...

  8. 【bzoj 3173】[Tjoi2013]最长上升子序列

    Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...

  9. bzoj3173: [Tjoi2013]最长上升子序列(fhqtreap)

    这题用fhqtreap可以在线. fhqtreap上维护以i结尾的最长上升子序列,数字按从小到大加入, 因为前面的数与新加入的数无关, 后面的数比新加入的数小, 所以新加入的数对原序列其他数的值没有影 ...

最新文章

  1. vs2017 release模式断点调试
  2. Python数据格式:%s字符串,%d整型,%f浮点型
  3. 关于浮点型加减乘除运算不精确的问题
  4. 针对access数据库的增删改查
  5. fread函数和fwrite函数,read,write
  6. 解决uni-app官方弹框popup关闭不了问题;/pages/extUI/popup/popup;uni-app弹框popup打开调用事件。unin-app弹框封装;
  7. mysql binlog查看_MySQL--17 配置binlog-server 及中间件
  8. android_secure写权限,android.permission.WRITE_SECURE_SETTINGS权限报错
  9. UESTC 1143 数据传输 网络流 最大流 Dinic
  10. 深度学习自学(三):NMS非极大值抑制总结
  11. 学会这两个技巧!让你的Mac文件共享超简单
  12. Compile opencv 2.4.9/2.4.10.1 as static library
  13. 计算机中submit commit区别
  14. linux 945gse 显卡驱动,HP520的945Expres集成显卡只能以1024x768的模式显示的求助
  15. android 命令pm 全称 packagemanager,Android.content.pm.PackageManager--(转载)
  16. Intellij IDEA 插件下载慢或无法查询
  17. sync.Once 使用及解析
  18. ALtera DE2开发板学习01
  19. How to test Neutron VRRP HA rapidly (by quqi99)
  20. 小程序踩坑日志(一)

热门文章

  1. Nature Neuroscience|群际冲突的脑间同步机制
  2. 时空大数据可视化表达分析,看MapGIS七大“超能力”
  3. 中国AI创新者论坛将于3月21日在清华大学举办
  4. 一文读懂5G:颠覆生活资费天价?
  5. 社会科技奖不是新鲜事?如何真正做大
  6. 大咖 | 斯坦福教授骆利群:为何人脑比计算机慢1000万倍,却如此高效?
  7. 谷歌称居家办公影响工作效率!2021 年将回归线下办公
  8. 冒号课堂 编程范式与OOP思想
  9. 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
  10. 源码安装apache实例