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. Grid 拾遗

    *.GridSplitter属性是Grid面板的一个特性.(1)预留一行或一列,专门用于放置 GridSplitter对象,如果是行的话,把其Height 设置为 Auto,如果是列的话,把其 Wid ...

  2. How to Build a Search Page with Elasticsearch and .NET

    Although SQL Server's Full-Text search is good for searching text that is within a database, there a ...

  3. mac 苹果鼠标 magic mouse2 当触摸代替点击当触摸板教程

    本文解决 mac 苹果鼠标 magic mouse2 触摸代替点击,鼠标当触摸板教程 买了magic mouse2之后,发现官方不推荐使用触摸代替点击,我感觉很不爽,这不就是一个触摸板嘛,于是各种搜软 ...

  4. [Functional Programming] Arrow contramap vs map and promap

    In previous post, Arrow Functor with contramap, we have seen how to opreating on params before we in ...

  5. 【Nodejs】外研社一年级起三年级下MP3下载爬虫1.00

    //====================================================== // 外研社一年级起三年级下MP3下载爬虫1.00 // 2018年5月9日 //== ...

  6. MATLAB中的集合运算

    matlab里关于集合运算和二进制数的运算的函数 intersect:集合交集ismember :是否集合中元素setdiff :集合差集setxor :集合异或(不在交集中的元素)union :两个 ...

  7. WDCP,LNMP安装PHP缓存加速扩展eAccelerator,xcache和memcached

    VPS主机性能配置如果太差,则在运行Wordpress博客和Discuz! 论坛等高消耗程序时能够明显感觉出VPS有些吃力.另外,即使VPS主机的CPU.内存.硬盘I/O等性能足够好,但是在面对大流量 ...

  8. @SuppressLint("NewApi")和@TargetApi()的区别

    在Android代码中,我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,此时编译器会提示警告, 解决方法是在方法上加上@Suppr ...

  9. SQL Server Window Function 窗体函数读书笔记一 - SQL Windowing

    SQL Server 窗体函数主要用来处理由 OVER 子句定义的行集, 主要用来分析和处理 Running totals Moving averages Gaps and islands 先看一个简 ...

  10. Mybatis 自动生成代码,数据库postgresql

    最近做了一个项目,使用Mybatis自动生成代码,下面做一下总结,被以后参考: 一.提前准备: 1.工具类:mybatis-generator-core-1.3.2.jar 2.postgresql驱 ...