HDU 2227 Find the nondecreasing subsequences(DP)

HDU 2227 Find the nondecreasing subsequences(DP)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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

 
题意:问你不降子序列的个数。

一看n达到了1e5的级别。就知道得nlogn算法。

然而想到了一个mp的迭代可是每次迭代都得log复杂度太高。所以树状数组+离散化搞。

题解:设dp[i]为前i个而且以i结尾的的不降子序列个数。

我们知道前面凡是小于等于a[i]的都能够到dp[i],所以dp[i]+=dp[j](a[j]<=a[i]&&j<i).

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int MOD=1e9+7;
const int maxn=1e5+10;
int a[maxn],n,b[maxn+100];
LL dp[maxn],c[maxn+100];
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,LL val)
{
    while(x<maxn)
    {
        c[x]=(c[x]+val)%MOD;
        x+=lowbit(x);
    }
}
LL query(int x)
{
    LL sum=0;
    while(x>0)
    {
        sum=(sum+c[x])%MOD;
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int cnt=1;
        CLEAR(c,0);
        REPF(i,1,n)
        {
           scanf("%d",&a[i]);
           b[cnt++]=a[i];
        }
        sort(b+1,b+cnt);
        cnt=unique(b+1,b+cnt)-(b+1);
        for(int i=1;i<=n;i++)
            dp[i]=1;
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            int x=lower_bound(b+1,b+1+cnt,a[i])-b;
            dp[i]=(dp[i]+query(x))%MOD;
            update(x,dp[i]);
            ans=(ans+dp[i])%MOD;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

/*

*/

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117125.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • neokylin操作系统_kinit命令

    neokylin操作系统_kinit命令基础命令进入根目录cd/新建用户useraddname切换用户suname设置用户密码passwdname创建目录mkdirdirname目录删除(强制)rm(-rf)dirname文件创建touchfilename查看当前目录下的文件ll(-a查看隐藏文件)pwd查看当前目录或文件位置创建文件Touchaa.dataVimaaa.dataechomljs>data.log文件查看catfilename仅查看vimf

    2022年8月10日
    6
  • Docker EE 统一应用程序管理功能大揭秘,你想要的都在这里!

    Docker EE 统一应用程序管理功能大揭秘,你想要的都在这里!

    2021年6月7日
    147
  • mysql全文索引实现搜索功能(关键词查询)

    mysql全文索引实现搜索功能(关键词查询)最近在做一个关键词查询功能。所以开始了解mysql的全文索引技术。接下来我将一步一步告诉大家。我是如何一步一步实现关键词检索的。1.了解到mysql全文检索是以词为基础的。MySQL默认的分词是所有非字母和数字的特殊符号都是分词符。所以我存在数据库的样子是这样的。(左边的字段用于显示,右边的字段用于全文查询)2.全文检索的sqlSELECT*FROMtbk_item_coupon…

    2022年6月21日
    51
  • mysql 卸载及安装[通俗易懂]

    mysql 卸载及安装[通俗易懂]mysql卸载及安装

    2022年9月29日
    4
  • 因果图分析法[通俗易懂]

    因果图分析法[通俗易懂]目录一、因果图法1.理解二、因果图需要掌握的基本知识1.关系2.约束3.输出条件的约束4.输出条件的约束5.原因和结果表示6.中间节点三、因果图设计测试用例的步骤四、优缺点1.优点2.缺点五、实例1.案例2.分析案例六、为什么要有中间节点1.无中间节点因果图2.有中间节点因果图一、因果图法1.理解因果图是一种简化了的逻辑图,能直观的表明程序输入条件(原因)和输出动作(结果)之间的相互关系; 因果图法是借助图形来设计测试

    2022年8月14日
    7
  • 在flask中使用jsonify和json.dumps的区别

    在flask中使用jsonify和json.dumps的区别flask提供了jsonify函数供用户处理返回的序列化json数据,而python自带的json库中也有dumps方法可以序列化json对象,那么在flask的视图函数中return它们会有什么不同之处呢?想必开始很多人和我一样搞不清楚,只知道既然框架提供了方法就用,肯定不会错。但作为开发人员,我们需要弄清楚开发过程中各种实现方式的特点和区别,这样在我们面对不同的需求时才能做出相对合理的选择,而

    2022年5月24日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号