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)
上一篇 2022年1月10日 下午2:00
下一篇 2022年1月10日 下午3:00


相关推荐

  • python安装win32模块

    python安装win32模块pipinstallpy 不过清华源和豆瓣源都会安装报错 最后直接用 pycharm 安装成功了 PACKAGECONTE nbsp nbsp win32sysload nbsp nbsp winxptheme nbsp nbsp mmapfile nbsp nbsp odbc nbsp nbsp perfmon nbsp nbsp servicemanag nbsp nbsp timer nbsp nbsp win2kras nbsp nbsp win32api nbsp nbsp win32clip

    2026年3月17日
    2
  • 我也说说Nginx解决前端跨域问题,正确的Nginx跨域配置(后端Nginx CORS跨域配置、CORS设置,后端允许跨域请求)

    我也说说Nginx解决前端跨域问题,正确的Nginx跨域配置(后端Nginx CORS跨域配置、CORS设置,后端允许跨域请求)最近连续两个朋友问我跨域相关问题 我猜想可能不少朋友也遇到类似问题 我打算写个博客聊一下我实际使用的配置 先说明一下 我并不太了解这配置 没精力去了解太多 但我觉得其中有一些关键的小注意点 可能有些初学者不太注意到 导致配置有问题 本文章可能只对新手有点帮助 如果你有好配置 欢迎评论回复 让大家学习 Nginx 的 CORS 配置 网上太多这配置了 但大家更多的复制粘贴 转发 几乎都是类似下面这

    2026年3月19日
    2
  • org.apache.jasper.JasperException: org.apache.jasper.JasperException: java.lang.ClassNotFoundExcepti[通俗易懂]

    org.apache.jasper.JasperException:org.apache.jasper.JasperException:java.lang.ClassNotFoundException:org.apache.jsp.jsp.main_jsp主要问题:原因:有多个界面有下面这句话<%@tagliburi="http://java.sun.com/jstl/core"

    2022年4月7日
    365
  • app产品设计流程_APP流程图

    app产品设计流程_APP流程图App设计流程第一步、从APP产品需求入手,考虑我们到底要用什么主色调根据产品定位和目标用户群体选择主色调定好尺寸:设计尺寸是多大,是以640*1136设计还是750*1136还是1242*2208来设计。所有APP设计尺寸大小规范:http://www.25xt.com/appsize   第二步、配色和辅助色用什么颜色在考虑到产品气质和品牌色的同时,我们经常要考虑配合衬托产品主色调的辅助色…

    2022年8月30日
    8
  • Python+Requests+Pytest 接口自动化测试脚本总结

    Python+Requests+Pytest 接口自动化测试脚本总结1 封装 post 和 get 方法 方便在使用 requests 模块发送请求时 仅调用一个方法即可备注 文件名均在脚本中的顶部 用 fileName 标识 usr bin envpython coding utf 8 fileName run method pyimportrequ object de

    2026年3月18日
    2
  • java对象转换map

    java对象转换map背景介绍原理说明反射概念功能作用实现方式方法介绍实例展示对象转MAP背景介绍  今天在项目研发的过程中遇到这样一个需求,在一个统一处理类的入口要将所有后面处理流程需要用到的值统一塞进上下文的MAP对象中,这其中就包括了一持久层的DO对象。  如果对于对象进行逐个遍历是可以实现这个需求,但代码量比较大,所以一直在寻求一种比较合理的处理方式。后来发现可以通过反射的方式实现这个功能。原理

    2022年6月11日
    44

发表回复

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

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