题目链接: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. .NET+IIS+MSSQL配置

    好久没配置.NET+IIS+MSSQL了,跟以前不大一样了.总结下吧. 环境: Windows Server 2012 标准版 x64 SQL Server Express 2014 一.HTTP E ...

  2. gnuWin32-mini-2016.10.30

    2016-10-28 04:48 1,017,856 awk.exe ver 4.1.4 2016-10-29 00:26 77,312 bc.exe ver 1.06 2016-10-30 01:4 ...

  3. sql跨库查询

    ---------------------------------------------------------------------------------- --1. 创建链接服务器 --1. ...

  4. extern

    gcc编译器编译程序有四个阶段,预处理.编译.汇编.链接.预处理阶段会将源代码中的包含的头文件如stdio.h编译进来:编译阶段,gcc首先要检查代码的规范性.是否有语法错误等,以确定代码的实际要做的 ...

  5. 使用Visual Leak Detector for Visual C++ 捕捉内存泄露

    什么是内存泄漏? 内存泄漏(memory leak),指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段 ...

  6. vs2013在使用ReportView11时遇到的问题

    最近在做项目中用到2013中的ReportView11  在本机IIS中使用完全没问题  但是放到服务器上总是出问题 解决办法:(1)首先在自己机器上开发的时候  是不用引用  Microsoft.R ...

  7. android 实现返回键执行home键方法

    在公司的产品开发,中老板很是执着于,不要看到启动界面,但是又不想去掉启动界面. so 实现返回键执行home键方法. public boolean onKeyDown(int keyCode, Key ...

  8. Centos7下安装mysql5.7.16

    mysql的安装(root用户下) 从官网下载软件 linux下必须安装系统对应的版本,多少位 必须安装的是:server,client 但是我可不管要安装那个插件,我们直接使用bundle版本(就是 ...

  9. Maven之安装与简单入门一

    Maven 是一个项目管理和构建自动化工具,我们最关心的是它的项目构建功能.Maven 使用惯例优于配置的原则. 1,下载安装包,并根据文档说明安装: http://maven.apache.org/ ...

  10. GRUB、MBR名词解释

    GRUB:是一个来自GUN项目的多操作系统启动程序,是多启动规范的实现,他允许用户在计算机内同时拥有多个操作系统,并在计算机启动时选择希望的操作系统.GRUB可用于选择系统分区上的不同内核,也可用于向 ...