Find the nondecreasing subsequences

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1844    Accepted Submission(s): 677

Problem Description
How
many nondecreasing subsequences can you find in the sequence S = {s1,
s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you
can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1,
3}, {2, 3}, {1, 2, 3}.
 
Input
The
input consists of multiple test cases. Each case begins with a line
containing a positive integer n that is the length of the sequence S,
the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n
<= 100000, 0 <= si <= 2^31.
 
Output
For
each test case, output one line containing the number of nondecreasing
subsequences you can find from the sequence S, the answer should %
1000000007.
 
Sample Input
3
1 2 3
 
Sample Output
7
 
题意:找到一个字符串里面所有的不下降子序列的数量之和。
题解:设dp[i] 以a[i]结尾的非递减子串的个数,可以知道 dp[i] = sum(dp[j])+1 (1<=j<i,a[j]<a[i]),这里a[j]<a[i]我们可以直接利用求解逆序数的原理得到,利用树状数组维护整个递推式。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
const int mod = ;
int n;
int a[N],b[N],c[N];
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=n;i+=lowbit(i)){
c[i]=(c[i]+v)%mod;
}
}
int getsum(int idx){
int sum = ;
for(int i=idx;i>=;i-=lowbit(i)){
sum = (sum+c[i])%mod;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF){
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(b+,b++n);
int ans = ;
for(int i=;i<=n;i++){
int idx = lower_bound(b+,b++n,a[i])-b;
ans=getsum(idx);
update(idx,ans+);
}
printf("%d\n",getsum(n));
}
return ;
}

最新文章

  1. mysql 5.7.15单机主从快速搭建并配置复制表到不同库
  2. 简单的js菜单
  3. js部分总结
  4. socketserver模块写的一个简单ftp程序
  5. mybatis实战
  6. Jetty 与 Tomcat 比较,及性能分析
  7. axure rp pro 6.5
  8. ANDROID_MARS学习笔记_S01原始版_010_ContentProvider
  9. springboot 1.5.2 集成kafka 简单例子
  10. python之路第四篇(基础篇)
  11. Android动态加载技术(插件化技术)
  12. Script error.解决方法
  13. Visual Stuido Online:如何禁止多人同时签出同一文件
  14. jquery 語法
  15. [TensorBoard] Name &amp; Variable scope
  16. [转]Hadoop Hive sql语法详解
  17. 《C++ Primer Plus》16.2 智能指针模板类
  18. ZooKeeper 集群的安装、配置---Dubbo 注册中心
  19. WeX5入门之欢乐捕鱼打包
  20. haproxy有关session的问题

热门文章

  1. ContestHunter暑假欢乐赛 SRM 01 - 儿童节常数赛 爆陵记
  2. 【状压DP】【UVA11825】 Hackers&#39; Crackdown
  3. Linux之初试驱动20160613
  4. layui中对表格操作按钮集的判断
  5. matlab特殊符号表
  6. HDU2819:Swap(二分图匹配)
  7. springmvc不通过controller进行页面跳转
  8. 【转载】Innodb共享表空间VS独立表空间
  9. udhcpd源码分析2--读取配置文件
  10. 用HTML5 File API 实现截图粘贴上传、拖拽上传