题目链接: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. mysql 存储过程 事务; mysql的事务中包含一个存储过程

    在asp.net结合mysql的开发中,我平时用到的事务处理是 使用 TransactionOptions  来进行处理 TransactionOptions transactionOption = ...

  2. C++实现日期类(Date类)

    #include<iostream>using namespace std;class Date{public:    Date(int year = 1900, int month = ...

  3. BZOJ 4408 神秘数

    题解同各神犇的方法... #include<iostream> #include<cstdio> #include<cstring> #include<alg ...

  4. Linux Shell编程(3):数组

    http://snailwarrior.blog.51cto.com/680306/154704 BASH只支持一维数组,但参数个数没有限制.   声明一个数组:declare -a array (其 ...

  5. 【英语】Bingo口语笔记(68) - come系列

  6. 【转】Linux设备驱动之Ioctl控制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275372.html 大部分驱动除了需要具备读写设备的能力之外,还需要具备对硬件控制的能 ...

  7. 你能识别这些科技公司的真假logo吗?

    快告诉我,不止我一个眼瞎~

  8. nginx的配置,要求根据不同的来路域名,发送到不同的端口去处理

    这一台电脑上既有tomcat 也有 apache,他俩是没有办法同时享用80端口的.我现在让tomcat用8088,apache用8080,然后让nginx用80,这样nginx在收到请求后,根据不同 ...

  9. Android学习笔记--Sqlite数据库

    前几天学习了Android中的数据存储,包括文件存储,SharedPreferences存储,还有就是Acndroid中的特色:SQLite数据库存储了.让我比较惊讶的是Android中竟然内嵌了一个 ...

  10. ASCII和16进制

    所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...