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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Laravel 出现 No application encryption key has been specified.

    Laravel 出现 No application encryption key has been specified.

    2021年10月20日
    39
  • URL Escape Codes 方便日后查询

    URL Escape Codes 方便日后查询

    2021年5月9日
    149
  • 一分钟制作U盘版BT3 – 有图滴儿 bt3破解教程

    一分钟制作U盘版BT3 – 有图滴儿 bt3破解教程

    2021年12月10日
    47
  • Keil中代码的颜色设置 ( 很 全 )[通俗易懂]

    Keil中代码的颜色设置 ( 很 全 )[通俗易懂]因为长时间要编程,对于keil上的黑字白底,如果看久了会让人眼睛产生疲倦感,所以最好将代码颜色和底色都改变一下,让自己看起来舒服一点。下面是文字图片教程(如果自己没空去设置,我后面也有两套模板可以直接用,教程也在后面)1、点击keil右上角edit——Configuration——Color&Fonts(上面标签)2、3、(注意事…

    2022年5月10日
    223
  • 操作系统——银行家算法

    操作系统——银行家算法自从写完第一篇博客,发现写博客也挺好玩的,比平时写word应付作业有趣的多,而且文章在网上还能帮助别人,自己平时也经常看CSDN,这不,老师要求我们实现一下操作系统的银行家算法,所以我就来了!那么,什么是银行家算法呢?如果你很了解请跳过这一段,就是解决死锁问题的一个算法,是由艾兹格·迪杰斯特拉在1965年为T.H.E系统设计的一种避免死锁产生的算法。它以银行借贷系统的分配策略为基础,判…

    2022年7月22日
    10
  • 新利EagleEye信息平台保障系统(服务器监控系统)[摘]

    新利EagleEye信息平台保障系统(服务器监控系统)[摘]新利EagleEye信息平台保障系统(服务器监控系统)1 系统概述  新利EagleEye信息平台保障系统(服务器监控系统)是一款集服务器、业务应用和网络设备、环境动力的监控和管理于一体的集中式、跨平台的信息运营平台保障管理平台系统。系统实现监测的自动化和故障预警智能化管理,故障准确定位和综合诊断IT系统异常的原因,最大限度地保障IT信息系统的正常运行,减少运维部门的运维成本,实现信息维护服

    2022年8月16日
    8

发表回复

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

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