http://acm.hdu.edu.cn/showproblem.php?pid=5542

题意:求严格递增的长度为M的序列的组数。

当dp的优化方案不那么容易一眼看出来的时候,我们可以考虑先写一个朴素算法,在朴素算法的基础上去考虑优化。

正如这题,很显然用dp[i][j]存储长度为i的序列以j结尾的情况。

然后有两种方法去递推。

一种是从1--M序列的长度,对于每一个数字去寻找他前面比她小的数列进行递推,递推方程dp[i][j] += dp[i - 1][k]; (k < j && a[k] < a[j])

第二种方法是从1--N枚举每个数,对于每个数直接寻找他前面的所有满足上面k条件的数字进行递推。

两种方法时间复杂度相同,朴素算法都是T * M * N * N,显然会TLE,但是考虑在其中一层,也就是寻找他前面枚举条件的数这一步进行优化,当我们用树状数组维护前缀的和的时候,我们就可以把一层N优化变为lnN,总的时间复杂度变成O(TNMln(N));

        For(i,,M){
Mem(tree,);
if(i == ) add(,);
For(j,,N){
dp[i & ][j] = getsum(a[j] - ) % mod;
add(a[j],dp[i + & ][j]);
}
}

第一种方法的主要优化代码

    For(i,,N){
For(j,,M){
if(j == ) dp[j & ][i] = ;
else dp[j & ][i] = getsum(j - ,a[i] - ) % mod;
add(j,a[i],dp[j & ][i]);
}
}

第二种方法的主要优化代码

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = ;
const LL INF = 1e18;
const int mod = 1e9 + ;
int N,M,tmp,K;
LL a[maxn];
int cnt;
LL Hash[maxn];
inline LL read()
{
LL now=;register char c=getchar();
for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*+c-'',c=getchar());
return now;
}
LL dp[][maxn];
LL tree[maxn];
void add(int x,LL t){
for(;x <= cnt + ; x += x & -x) tree[x] = (tree[x] + t) % mod;
}
LL getsum(int x){
LL s = ;
for(;x > ;x -= x & -x) s = (s + tree[x]) % mod;
return s;
}
int main()
{
int T; Sca(T);
int CASE = ;
while(T--){
Sca2(N,M);
For(i,,N) Hash[i] = a[i] = read();
sort(Hash + ,Hash + + N);
cnt = unique(Hash + ,Hash + + N) - Hash - ;
For(i,,N) a[i] = lower_bound(Hash + ,Hash + + cnt,a[i]) - Hash + ;
Mem(dp,);
For(i,,M){
Mem(tree,);
if(i == ) add(,);
For(j,,N){
dp[i & ][j] = getsum(a[j] - ) % mod;
add(a[j],dp[i + & ][j]);
}
}
LL ans = ;
For(i,,N) ans = (ans + dp[M & ][i]) % mod;
printf("Case #%d: ",CASE++);
Prl(ans);
}
#ifdef VSCode
system("pause");
#endif
return ;
}

HDU5542 BIT优化dp的更多相关文章

  1. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  2. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  3. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  4. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  5. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  6. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  7. 优化DP的奇淫技巧

    DP是搞OI不可不学的算法.一些丧心病狂的出题人不满足于裸的DP,一定要加上优化才能A掉. 故下面记录一些优化DP的奇淫技巧. OJ 1326 裸的状态方程很好推. f[i]=max(f[j]+sum ...

  8. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  9. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

随机推荐

  1. 开始一个简单的ASP.NET Web API 2 (C#)

    创建一个Web API 项目 在本教程中,你将使用ASP.NET Web API 来创建一个web API 并返回产品列表. 网页前端使用jQuery 显示结果. 选择ASP.NET Web Appl ...

  2. Tyche 2317 Color

    题目大意:有三个人alice,bob,yazid,三种颜色red,blue,green,每个人对应一种颜色. [name] is [color]. Yazid会做以下操作: 1 将三个句子连在一起 2 ...

  3. linux缺失gcc的安装方法

    linux安装gcc操作 1.查看linux是否有gcc文件 这个是没有挂载的 2. 使用df,查看系统光盘的挂载位置 3.卸载分区 umount /dev/sr0 4.将redhat系统光盘重新载入 ...

  4. [IOI2018]组合动作——构造

    题目连接: [IOI2018]combo 题目大意:有一个未知的长度为n的字符串$T$,只包含$A,B,X,Y$四个字符且首字母只出现一次,每一次你可以询问一个长度不超过$4n$的字符串$S$,交互库 ...

  5. python成长之路二

    python的print格式化输出,以及使用format来控制. 1,打印字符串(str),利用%s. >>> print ('My name is %s' % ('TaoXiao' ...

  6. 【BZOJ5119】【CTT2017】生成树计数 DP 分治FFT 斯特林数

    CTT=清华集训 题目大意 有\(n\)个点,点权为\(a_i\),你要连接一条边,使该图变成一颗树. 对于一种连边方案\(T\),设第\(i\)个点的度数为\(d_i\),那么这棵树的价值为: \[ ...

  7. 【XSY2472】string KMP 期望DP

    题目大意 给定一个由且仅由字符'H','T'构成的字符串\(S\). ​ 给定一个最初为空的字符串\(T\) ,每次随机地在\(T\)的末尾添加'H'或者'T'. 问当\(S\)为\(T\)的后缀时, ...

  8. readlink: command not found 解决方案

    /c/Program Files (x86)/Yarn/bin/yarn: line 3: readlink: command not found 用gitbash运行yarn时提示这个错误,但没有直 ...

  9. 【agc030f】Permutation and Minimum(动态规划)

    [agc030f]Permutation and Minimum(动态规划) 题面 atcoder 给定一个长度为\(2n\)的残缺的排列\(A\),定义\(b_i=min\{A_{2i-1},A_{ ...

  10. css- @media @font-face 的理解

    在我的博客园定制的css中有2个之前没有遇到的csss属性@font-face @media @media 在css文件中的使用如下 ` @media screen and (max-width: 1 ...