题目链接:http://acm.uestc.edu.cn/#/problem/show/1217

给你一个长为n的数组,问你有多少个长度严格为m的上升子序列。

dp[i][j]表示以a[i]结尾长为j的上升子序列个数。常规是三个for。

这里用树状数组优化一下,类似前缀和的处理,两个for就好了。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e3 + ;
LL dp[N][N], mod = 1e9 + ;
int a[N], b[N], m, n;
LL bit[N][N]; void add(int pos, int i, int val) { //上升子序列长度为pos
for( ; i <= n; i += (i&-i))
bit[pos][i] = (bit[pos][i] + val) % mod;
} LL sum(int pos, int i) {
LL s = ;
for( ; i >= ; i -= (i&-i))
s = (s + bit[pos][i]) % mod;
return s;
} int main()
{
int t;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
b[i] = a[i];
}
sort(b + , b + n + );
for(int i = ; i <= n; ++i) {
a[i] = lower_bound(b + , b + n + , a[i]) - b; //离散化
}
memset(dp, , sizeof(dp));
memset(bit, , sizeof(bit));
dp[][] = ;
add(, a[], );
for(int i = ; i <= n; ++i) {
dp[i][] = ;
for(int k = max(m - (n - i), ); k <= min(i, m); ++k) { //这边可以优化一下
dp[i][k] = (dp[i][k] + sum(k - , a[i] - )) % mod; //比a[i]小且上升子序列长度为k-1
add(k, a[i], dp[i][k]);
}
}
LL res = ;
for(int i = ; i <= n; ++i) {
res = (res + dp[i][m]) % mod;
}
printf("Case #%d: %lld\n", ca, res);
}
return ;
}

uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)的更多相关文章

  1. HDU - 5542 The Battle of Chibi(LIS+树状数组优化)

    The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...

  2. C - The Battle of Chibi HDU - 5542 (树状数组+离散化)

    Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about ...

  3. [luoguP3402] 最长公共子序列(DP + 离散化 + 树状数组)

    传送门 比 P1439 排列LCS问题,难那么一点点,只不过有的元素不是两个串都有,还有数据范围变大,树状数组得打离散化. 不过如果用栈+二分的话还是一样的. ——代码 #include <cs ...

  4. The Battle of Chibi(数据结构优化dp,树状数组)

    The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...

  5. HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...

  6. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. 【CF314C】Sereja and Subsequences(DP,树状数组)

    题意:给定一个N个数的数列,求所有不同不下降子序列的乘积之和,其中不同指的是组成它的数字和长度不完全相同 n (1 ≤ n ≤ 10^5) a[i]<=10^6 思路:考虑DP.设DP[a[i] ...

  8. HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...

  9. 洛谷P2305 [NOI2014]购票 [DP,树状数组]

    传送门 思路 显然是树形DP,显然是斜率优化,唯一的问题就是该怎么维护凸包. 套路1:树上斜率优化,在没有这题的路程的限制的情况下,可以维护一个单调栈,每次加入点的时候二分它会加到哪里,然后替换并记录 ...

随机推荐

  1. [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁

    转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...

  2. 一、HTML4背景知识

    前言 本教程针对已经基本熟悉HTML4基本标签的人. HTML的发展历程 HTML 1.0: 1993年6月由IETF(Internet Engineering Task Force, 互联网工程工作 ...

  3. Oracle安装错误ora-00922(zhuan)

    Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈   安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...

  4. IntelliJ IDEA使用总结篇

    解决控制台中文乱码的问题: 1.windows下改intellij安装目录下bin\idea.exe.vmoptions文件 加上 -Dfile.encoding=UT 2.设置IDEA server ...

  5. python练习程序(显示图像)

    import matplotlib as mpl import Image import numpy as np import matplotlib.pyplot as plt im=Image.op ...

  6. (三)用Normal Equation拟合Liner Regression模型

    继续考虑Liner Regression的问题,把它写成如下的矩阵形式,然后即可得到θ的Normal Equation. Normal Equation: θ=(XTX)-1XTy 当X可逆时,(XT ...

  7. Struts2 的 helloworld

    配置步骤: 1.在你的strut2目录下找到例子项目,把它的 lib 下的jar拷贝到你的项目.例如我的:struts-2.3.24\apps\struts2-blank 2.struts-2.3.2 ...

  8. 细数Android开源项目中那些频繁使用的并发库中的类

    这篇blog旨在帮助大家 梳理一下前面分析的那些开源代码中喜欢使用的一些类,这对我们真正理解这些项目是有极大好处的,以后遇到类似问题 我们就可以自己模仿他们也写 出类似的代码. 1.ExecutorS ...

  9. hdu 3032(博弈sg函数)

    题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...

  10. 几种ESB(企业服务总线)介绍

    ESB(Enterprise Service Bus,即企业服务总线)是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素. 企业服务 ...