题意

给定$n$个数,求最长上升子序列的方案数


根据数据范围要求是$O(n\log n)$

朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数

从通酱博客里还看到更详尽的解释:stackoverflow

时间复杂度$O(n\log n)$

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair<int,int> pii;
  4. int n,m,readin;
  5. pii f[100005];
  6. pii query(int x) {
  7. pii ret=make_pair(0,1);
  8. while(x) {
  9. if(f[x].first>ret.first)ret=f[x];
  10. else if(f[x].first==ret.first)ret.second=(ret.second+f[x].second)%m;
  11. x-=x&(-x);
  12. }
  13. return ret;
  14. }
  15. void add(int x,pii v) {
  16. while(x<=n) {
  17. if(f[x].first<v.first)f[x]=v;
  18. else if(f[x].first==v.first)f[x].second=(f[x].second+v.second)%m;
  19. x+=x&(-x);
  20. }
  21. }
  22. int main() {
  23. scanf("%d%d",&n,&m);
  24. for(int i=1;i<=n;++i) {
  25. scanf("%d",&readin);
  26. pii t=query(readin);
  27. t.first++;
  28. add(readin,t);
  29. }
  30. printf("%d\n",query(n).second);
  31. return 0;
  32. }

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组的更多相关文章

  1. 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences

    求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...

  2. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  3. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  6. Codeforces 960F Pathwalks ( LIS && 树状数组 )

    题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 :  这题和 LIS 很相似,不同的 ...

  7. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  8. CF 314C Sereja and Subsequences(树状数组)

    题目链接:http://codeforces.com/problemset/problem/314/C 题意:给定一个数列a.(1)写出a的不同的所有非下降子列:(2)定义某个子列的f值为数列中各个数 ...

  9. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

随机推荐

  1. 【Python】ModuleNotFoundError: No module named 'matplotlib.pyplot'

    安装好matplotlib后,很激动的建立了一个文件夹matplotlib,并且在其下面建立了,mpl_squraes.py文件,代码编辑完成以后,点击运行,报错如下: 仔细分析了之后,发现是文件夹名 ...

  2. js 小总结

    数组操作 创建数组:var standTerm = new Array("维护","维修"); var arr = new Array(); 数组长度:leng ...

  3. springMVC --@RequestParam注解(后台控制器获取參数)

    在SpringMVC后台控制层获取參数的方式主要有两种,一种是request.getParameter("name"),第二种是用注解@RequestParam直接获取. 1.获取 ...

  4. 安装WordPress

    安装php yum install php-fpm yum install php systemctl start php-fpm 启动php nginx 配置

  5. 系统安全-Google authenticator

    对于某些人来说,盗取密码会比你想象的更简单  以下任意一种常见的操作都可能让你的密码面临被盗的风险: 在多个网站上使用同一个密码 从互联网上下载软件 点击电子邮件中的链接 两步验证可以将别有用心的人阻 ...

  6. Selenium3 Python3 Web自动化测试从基础到项目实战之二浏览器的不同设置

    在前面一个章节我们知道了如何通过webdriver去初始化我们得driver,然后我们只需要通过driver就能够去做我们得自动化,首先我们知道我们需要知道得是当我们有driver之后,我们剩下得就是 ...

  7. 13 nginx gzip压缩提升网站速度

    一:nginx gzip压缩提升网站速度 我们观察news.163.com的头信息 请求: Accept-Encoding:gzip,deflate,sdch 响应: Content-Encoding ...

  8. phpmyadmin内存溢出

    phpmyadmin Fatal error: Allowed memory size of 134217728 bytes 解决方法: 在报错的页面里,加上这句: ini_set('memory_l ...

  9. python 基础 8.1 r 正则对象

                                                                                                        ...

  10. python 基础 2.6 break用法

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...