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. javascript——获取列表选择行的列值

    表格布局 方法 代码 $("#send").click(function(){ //获取选择的行号 n= $('input:radio[name="radio" ...

  2. linux命令学习——file

    1.简介 file命令是用来检测并显示文件类型(determine file type).通过file指令,我们得以辨识该文件的类型,例如可以知道动态连接库是32位还是64位. 2.命令格式 file ...

  3. Node.js中针对中文的查找和替换无效的解决方法

    Node.js中针对中文的查找和替换无效的解决方法.   //tags的值: tag,测试,帖子 var pos1 = tags.indexOf("测"); //这里返回-1 ta ...

  4. 基于spring与mockito单元测试Mock对象注入

    转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...

  5. C 语言宏定义

    C 语言宏定义1.例子如下: #define PRINT_STR(s) printf("%s",s.c_str()) string str = "abcd"; ...

  6. Python操作Mysql实例代码教程在线版(查询手册)_python

    实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...

  7. ZH奶酪:Ionic通过angularJS+tabs-item-hide实现自定义隐藏tab

    参考链接:http://stackoverflow.com/questions/23991852/how-do-i-hide-the-tabs-in-ionic-framework 1.index.h ...

  8. VCAP5-DCA – What’s new?

    see also: 韩国人的教材:http://ddii.pe.kr/ Section 1.1 – Implement and Manage complex storage Determine use ...

  9. Mysql5.7初始化成空密码或随机密码的方式

    命令在此:mysqld --initialize-insecure --user=mysql 文档表明,使用-initialize生成随机密码,使用-initialize-insecure生成空密码. ...

  10. Cocos开发小游戏如何实现微信排行榜

    当游戏开发进入尾声,要引入微信数据的时候,需要了解和微信相关的接口了. 关系数据链 开放数据域 相关接口如下: wx.getFriendCloudStorage() 获取当前用户也玩该小游戏的好友的用 ...