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


相关推荐

  • 音视频传输基本知识[通俗易懂]

    音视频传输基本知识[通俗易懂]音视频传输时的基本步骤:1.发起会话(Sip协议)2.编码(硬件编码、软件编码)3.传输(RTP)4.解码(硬件解码、软件解码)5结束会话(Sip协议)视频格式视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类。尽管后者在播放的稳定性和播放画面质量上可能没有前者优秀,但网络流媒体影像视频的广泛传播性使之正被广泛应用于视频点播、网络演示

    2022年10月3日
    0
  • java实现仿QQ即时聊天[通俗易懂]

    java实现仿QQ即时聊天[通俗易懂]这是我的java大作业,这里就直接贴上我的实验报告了。1.1项目介绍这是一个模仿QQ的即时聊天软件,可以通过运行在本地的服务端,实现两个客服端之间的通信,即聊天。采用的是javafx架构作为GUI设计架构,个人认为优点是可以自己设计css,使界面达到美观的目的。本项目共有登录、注册、重置密码、主界面发消息、添加好友、好友列表项、查看聊天记录、删除聊天记录、未读消息提醒、好友主页、我的主页等模块…

    2022年5月15日
    96
  • 安卓ExpandableListView的详细使用教程(附代码解析过程)

    安卓ExpandableListView的详细使用教程(附代码解析过程)ExpandableListView又称可扩展的ListView,它可以实现点击父项展开子项的效果,本文实现了一个比较精美的ExpandableListView。

    2022年6月30日
    15
  • DDL和DML的含义

    DDL表示DataDefinitionLanguage数据定义语言,主要包括CREATE,ALTER,DROP;隐性提交的,不能rollback。DML表示DataManipulationLanguage数据操作语言,主要的DML有SELECT,INSERT,UPDATE,DELETE;可以手动控制事务的开启、提交和回滚的。…

    2022年4月6日
    52
  • Oracle创建表空间和创建临时表空间

    Oracle创建表空间和创建临时表空间/*第1步:创建临时表空间 */createtemporarytablespacekc_temptempfile’C:\app\Administrator\oradata\orcl\kc_temp.dbf’ size50m  autoextendon  next50mmaxsize20480m  extentmanagementlocal;   

    2022年7月27日
    1
  • Jquery 400报错

    Jquery 400报错问题:前端能够完整传递数据,后台不能相应的接收到所有的数据解决思路:1,前端传送的数据格式和后端接收的数据格式不一一对应,400报错2,修改前端界面的name属性,name的属性与后台的接收字段名称,3,如果使用实体接收数据的话,未接收到数据,则是数据类型的问题,传递过来的数据默认是String类型的数据,但是实体中有integer或者timestamp格式4,如果使用参数集合接收数据,…

    2022年6月7日
    34

发表回复

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

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