uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接: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 + 离散化 + 树状数组)的更多相关文章
- 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 ...
- 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 ...
- [luoguP3402] 最长公共子序列(DP + 离散化 + 树状数组)
传送门 比 P1439 排列LCS问题,难那么一点点,只不过有的元素不是两个串都有,还有数据范围变大,树状数组得打离散化. 不过如果用栈+二分的话还是一样的. ——代码 #include <cs ...
- 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 ...
- 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 ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 【CF314C】Sereja and Subsequences(DP,树状数组)
题意:给定一个N个数的数列,求所有不同不下降子序列的乘积之和,其中不同指的是组成它的数字和长度不完全相同 n (1 ≤ n ≤ 10^5) a[i]<=10^6 思路:考虑DP.设DP[a[i] ...
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- 洛谷P2305 [NOI2014]购票 [DP,树状数组]
传送门 思路 显然是树形DP,显然是斜率优化,唯一的问题就是该怎么维护凸包. 套路1:树上斜率优化,在没有这题的路程的限制的情况下,可以维护一个单调栈,每次加入点的时候二分它会加到哪里,然后替换并记录 ...
随机推荐
- mysql 存储过程 事务; mysql的事务中包含一个存储过程
在asp.net结合mysql的开发中,我平时用到的事务处理是 使用 TransactionOptions 来进行处理 TransactionOptions transactionOption = ...
- Jquery源码中的Javascript基础知识(一)
jquery源码中涉及了大量原生js中的知识和概念,文章是我在学习两者的过程中进行的整理和总结,有不对的地方欢迎大家指正. 本文使用的jq版本为2.0.3,附上压缩和未压缩版本地址: http://a ...
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- pandas
- 浅析Android中的消息机制(转)
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- JVM——垃圾收集算法
1.标记-清除算法 最基础的收集算法,如其名,算法为“标记”和“清除”两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象. 两个不足: 1)效率问题,标记和清除两个过程的效率 ...
- 【转】MIPS交叉编译环境的建立
原文网址:http://imgtec.eetrend.com/forum/2371 我觉得对于MIPS处理起来说最令新手头疼的应该就是编译环境的建立了,这点MIPS做的确实不是很好,不像ARM那样有许 ...
- dynamic_cast
作为四个内部类型转换操作符之一的dynamic_cast和传统的C风格的强制类型转换有着巨大的差别.除了dynamic_cast以外的转换,其行为的都是在编译期就得以确定的,转换是否成功,并不依赖被转 ...
- 使用模板时 error LNK2019: 无法解析的外部符号
类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...
- SQL注入常用语句
整形参数判断 1.直接加' 2.and 1=1 3. and 1=2 如果1.3运行异常 2正常就存在注入 字符型判断 1.直接加' 2.and '1'='1' 3. ...