题意:

给你一个序列a[],求它的不降子序列的个数

分析:

dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]);答案就是sum(dp[i])

但发现一个问题n很大O(n^2)肯定超时,想起前面做的两道题,用线段树优化,它是用维护的是和,可以用BIT优化,但又发现a[i]的值很大没法存,就又想到了离散化,恩这个题就解决了。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll a[N],bit[N],tmp[N],dp[N];
int n;
void add(int x,ll d){
while(x<=n){
bit[x]=(bit[x]+d)%mod;
x+=(x&(-x));
}
}
ll sum(int x){
ll num=;
while(x>){
num=(num+bit[x])%mod;
x-=(x&(-x));
}
return num;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=;i<=n;++i){
scanf("%I64d",&a[i]);
tmp[i]=a[i];
}
sort(tmp+,tmp+n+);
int len=unique(tmp+,tmp+n+)-tmp-;
/*for(int i=1;i<=len;++i)
cout<<tmp[i]<<endl;*/
memset(dp,,sizeof(dp));
memset(bit,,sizeof(bit));
ll total=;
for(int i=;i<=n;++i){
int pos=lower_bound(tmp+,tmp+len+,a[i])-tmp;
dp[i]=;
dp[i]=(dp[i]+sum(pos))%mod;
add(pos,dp[i]);
total=(total+dp[i])%mod;
}
printf("%I64d\n",total);
}
return ;
}

HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)的更多相关文章

  1. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  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. HDU 2227 Find the nondecreasing subsequences (线段树)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. HDU 2227 Find the nondecreasing subsequences (数状数组)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 2227 Find the nondecreasing subsequences

    题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...

  7. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  8. HDU 3506 (环形石子合并)区间dp+四边形优化

    Monkey Party Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Tot ...

  9. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. hdu 2227(树状数组+dp)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

随机推荐

  1. POJ 2400 Supervisor, Supervisee(KM)

    題目鏈接 題意 :N个部门和N个员工,每个部门要雇佣一个工人,部门对每个工人打分,从1~N,1表示很想要,N表示特别不想要,每个工人对部门打分,从1~N.1表示很想去这个部门,N表示特别不想去这个部门 ...

  2. POJ1258Agri-Net

    http://poj.org/problem?id=1258 题意 : john当上了镇长,打算给每个农场都连接网络,需要用最小的成本连接其他所有农场,所以要找最少的纤维连在一起,任何两个农场之间的距 ...

  3. [SQL Server系] -- 视图

    1:定义 从用户角度来看,一个视图是从一个特定的角度来查看数据库中的数据. 从数据库系统内部来看,一个视图是由SELECT语句组成的查询定义的虚拟表. 从数据库系统内部来看,视图是由一张或多张表中的数 ...

  4. cygwin如何断点续传

    对于Cygwin,如果想安装的东西比较多的话,推荐先选择“Download without installing”,下载完了再从本地安装. 好了,说关于断点续传.我所知道的是—— 网上有说法:下载失败 ...

  5. cv论文(SPARSE REPRESENTATION相关)

    上个博文我讲了一些CNN相关的论文,比较浅显都是入门知识,这节课来总结一些稀疏表示方面的文章.至于上个博文说到的要讲的sparse coding的知识,我将会放在Deep Learning的专题里面讲 ...

  6. 转:在MyEclipse+Hibernate

    源地址http://blog.renren.com/share/270733118/5528463779 1.实验环境准备 MyEclipse8.6 Tomcat6.0.20 MySQL5.1 ...

  7. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  8. Image.FrameDimensionsList 属性-----具体使用案例

    上一篇中说到了图片的具体产生以及属性,本篇主要是具体的使用,详情案例见下面的具体视图及代码 using System;using System.Collections.Generic;using Sy ...

  9. Java语言基本语法

    Java语言基本语法 一.标识符和关键字 标识符 在java语言中,用来标志类名.对象名.变量名.方法名.类型名.数组名.包名的有效字符序列,称为“标识符”: 标识符由字母.数字.下划线.美元符号组成 ...

  10. Oracle Create DBLink

    DROP PUBLIC DATABASE LINK ORA11G_DBLINK; CREATE   PUBLIC   DATABASE LINK ORA11G_DBLINKCONNECT TO SYS ...