Find the nondecreasing subsequences

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

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
 
Author
8600
 
Recommend
lcy
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int mod=;
const int N=; struct node{
int val,id;
}a[N]; int n,b[N],c[N],arr[N]; int lowbit(int x){
return x&(-x);
} void update(int i,int val){
while(i<=n){
arr[i]+=val;
arr[i]%=mod;
i+=lowbit(i);
}
} int Sum(int i){
int ans=;
while(i>){
ans+=arr[i];
ans%=mod;
i-=lowbit(i);
}
return ans;
} int cmp(node a,node b){
return a.val<b.val;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
memset(b,,sizeof(b));
memset(arr,,sizeof(arr));
for(int i=;i<=n;i++){
scanf("%d",&a[i].val);
a[i].id=i;
}
sort(a+,a+n+,cmp);
b[a[].id]=;
for(int i=;i<=n;i++)
if(a[i].val==a[i-].val)
b[a[i].id]=b[a[i-].id];
else
b[a[i].id]=i;
for(int i=;i<=n;i++){
c[i]=Sum(b[i]);
update(b[i],c[i]+);
}
printf("%d\n",Sum(n));
}
return ;
}

HDU 2227 Find the nondecreasing subsequences (数状数组)的更多相关文章

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

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

  2. HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...

  3. HDU 2227 Find the nondecreasing subsequences (线段树)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  4. HDU 2227 Find the nondecreasing subsequences(DP)

    Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...

  5. HDU 2227 Find the nondecreasing subsequences

    题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...

  6. hdu 1556 A - Color the ball 数状数组做法

    #include<bits/stdc++.h> using namespace std; ; int n; int c[maxn]; int lowbit(int x) { return ...

  7. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  9. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

随机推荐

  1. how to restrict copy paste in a Textbox, in MFC?

    [问题] I am developing a small application in MFC... there is a little problem..hope you guys would he ...

  2. 【Lua】LuaForWindows_v5.1.4-46安装失败解决方案

    下个补丁vcredist_x86.exe 可以到下面连接下载: https://download.csdn.net/download/tvcctv27tv/10344318

  3. SSH框架的基本整合

    SSH框架的基本整合 AOP注解方式 编写切面类(包括通知和切入点) 开启自己主动代理 JDBC模板技术 Spring提供模板技术,数据库的操作 以后编写DAO层,都能够继承JdbcDaoSuppor ...

  4. 解决Android Studio提示gradle project sync failed报错的解决方法

    运行的时候报错,提示:gradle project sync failed 1.打开AS,切换到project目录结构依次进入目录app->gradle->gradle-wrapper.p ...

  5. 【转载】TypeScript学习笔记——var与let

    var 与  let 都是TypeScript里的变量声明方式 两者非常相似,在语法不存在很大的差异,而在语义上存在很大的区别 块作用域 var有着些奇怪的作用域规则 例如 function f(ru ...

  6. iOS9 Error Domain=NSURLErrorDomain Code=-1022 App Transport Security (ATS)

    iOS 9在HTTP 访问时会出错  iOS9 Error Domain=NSURLErrorDomain Code=-1022 这时需要修改info.plist 文件 在Info.plist中添加N ...

  7. JavaScript String 对象常用方法

    <script type="text/javascript"> //concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. var str = &q ...

  8. 通过adb命令在Android设备中执行Java命令, 并调用so文件。

    一.难点一:无法复制so文件到/system/lib或者/vendor/lib下,提示只读 解决方法: 2.使用android device monitor放库进入到 /system/lib出现只读权 ...

  9. 利用mvn进行多环境配置

    代码里的resource信息有很多,代码里写死某一个环境的配置的话,有以下若干问题. 1. dev,不同的beta上,使用的resource信息不同. 2. 代码没有发布到对应的环境上,需要去机器上需 ...

  10. 【转】Android Studio开发应用桌面出现两个或多个图标

    原文链接:http://blog.csdn.net/jia635/article/details/78259699 解决办法: 查找的是不是自己的AndroidManifest中 多个Activity ...