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. [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined

    For example we have a component, it needs to call 'react-redux' connect function. import { compose, ...

  2. maven 配置篇 之pom.xml

    http://www.blogjava.net/zyl/archive/2006/12/30/91055.html http://maven.apache.org/pom.html的翻译.     m ...

  3. gzcms技术开发文档

    1.输出统一的json格式ajaxJSON.cs: 2.web.config注册html控件:3.gzcms.contrls开发控件库:4.form序列化提交$(this).serializeJson ...

  4. php 字符串中的\n换行符无效、不能换行的解决方法

    php 字符串中的\n换行符无效.不能换行的解决方法 程序的中的换行符\n会直接输出,无法正确换行,解决方法是把单引号改为双引号 aa

  5. ububuntu配置ip和dns

    装完ubuntu 第一件事情就是连上网,换个源,进行更新操作,但前提条件是要配好ip和dns. 下面把自己配置的过程记录下来,权且当作一份备份,以便不时之需. 一.配置ip ubuntu的网络配置信息 ...

  6. Wrong FS: hdfs://xxx/xxx expected: file:///

    Eclipse调用HDFS API上传文件时出现了如下错误: Exception in thread "main" java.lang.IllegalArgumentExcepti ...

  7. mysql标准写法及其他常见问题

    /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50549 Source H ...

  8. 如何查看页面是否开启gzip压缩

    F12 选择Network 表头右键: 如果开启了gzip则显示gzip,没有则是空. 上图是百度首页,显示已经进行gzip压缩.

  9. Android学习笔记三:用Intent串联activity

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...

  10. N-Queens I II(n皇后问题)(转)

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...