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

Find the nondecreasing subsequences

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

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,这里DP就是用树状数组慢慢推上去,最后是注意是求和会溢出,记得%1000000007。

 #include<iostream>
#include<stdio.h>
#include<memory.h>
#include<algorithm> using namespace std ; struct node
{
int val, id ;
}a[]; bool cmp(node a, node b)
{
return a.val < b.val ;
} int b[] , c[] , s[] ,n ; int lowbit( int i)
{
return i&(-i);
} void update ( int i , int x )
{
while(i<=n)
{
s[i]+=x;
if(s[i]>=)
s[i]%=;
i+=lowbit(i);
}
} int sum( int i )
{
int sum = ;
while( i > )
{
sum += s[i] ;
if( sum >= )
sum %= ;
i-=lowbit( i ) ;
}
return sum ;
} int main()
{
int i , res ;
while(scanf("%d",&n)!=EOF)
{
memset( b , , sizeof(b)) ;
memset( s , , sizeof(s)) ;
for(i=;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id = i ;
}
sort(a+,a+n+,cmp);
b[a[].id] = ;
for(i=;i<=n;i++)
{
if(a[i].val!=a[i-].val)
b[a[i].id] = i ;
else b[a[i].id] = b[a[i-].id] ;
}
res = ;
for(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 (DP+树状数组+离散化)的更多相关文章

  1. HDU 2227 Find the nondecreasing subsequences (数状数组)

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

  2. HDU 2227 Find the nondecreasing subsequences(DP)

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

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

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

  4. HDU2227Find the nondecreasing subsequences(树状数组+DP)

    题目大意就是说帮你给出一个序列a,让你求出它的非递减序列有多少个. 设dp[i]表示以a[i]结尾的非递减子序列的个数,由题意我们可以写出状态转移方程: dp[i] = sum{dp[j] | 1&l ...

  5. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  7. JZYZOJ 1360 [usaco2011feb]人品问题 DP 树状数组 离散化

    http://172.20.6.3/Problem_Show.asp?id=1360   好想好写   代码 #include<iostream> #include<cstdio&g ...

  8. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  9. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

随机推荐

  1. pullToRefreshListView的简单使用

    1.加入library后直接布局 library下载地址:http://pan.baidu.com/s/1dFJu8pF <com.handmark.pulltorefresh.library. ...

  2. erlang 分布式数据库Mnesia 实现及应用

    先推荐一篇:mnesia源码分析(yufeng)   - linear hash   ETS/DETS/mnesia 都使用了linear hash算法 http://en.wikipedia.org ...

  3. java 保留字符串数字的位数,不够前面补0

    @Test public void test() { this.printToConsole(autoGenericCode("10011")); this.printToCons ...

  4. 报错:1130-host ... is not allowed to connect to this MySql server

    报错:1130-host ... is not allowed to connect to this MySql server   解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在l ...

  5. Delphi Code Editor 之 编辑器选项

    Delphi Code Editor 之 编辑器选项 可从Code Editor的右键菜单中选择“Properties”菜单项来查看编辑器选项.也可以从主菜单[Tools | Editor Optio ...

  6. PHP多条件查询 (租房查询)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 在input中放对象

    var input = $("<input type='hidden' class='hidden-user'/>"); $(input).data("ran ...

  8. php阳历转农历的类 谷歌到的

    <?phpclass Lunar {    var $MIN_YEAR = 1891;    var $MAX_YEAR = 2100;    var $lunarInfo = array(   ...

  9. luac++

    Cocos2d-x下Lua调用自定义C++类和函数的最佳实践 洪亮 305 2014年08月09日 发布   1 推荐 21 收藏,14.9k 浏览 关于cocos2d-x下Lua调用C++的文档看了 ...

  10. iOS打包测试

    iOS开发中,怎么打包为ipa格式的软件包以及怎么安装到手机终端上. (我的开发环境为Xcode6.1.1 )作为个人开发者(当然我的开发者帐号为个人开发者帐号,而不是企业开发者帐号),对于自己来说, ...