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:树上斜率优化,在没有这题的路程的限制的情况下,可以维护一个单调栈,每次加入点的时候二分它会加到哪里,然后替换并记录 ...
随机推荐
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- CocoStudio UI 编辑器的使用
详细教程:http://www.cocoachina.com/bbs/read.php?tid=161567 Table of Contents 1 游戏中的 UI 1.1 基于 Cocos2d-x ...
- 理解 Bias 与 Variance 之间的权衡
有监督学习中,预测误差的来源主要有两部分,分别为 bias 与 variance,模型的性能取决于 bias 与 variance 的 tradeoff ,理解 bias 与 variance 有助 ...
- Activity传递对象的方法
//Serializeable传递对象的方法 public void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName ...
- Eclipse实用快捷键
经典常用快捷键1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. ...
- 15个必须知道的chrome开发者技巧(转)
15个必须知道的chrome开发者技巧 在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟 ...
- Apache Conf/Vost/xx.conf 文件配置
#监听8086端口Listen 8086 NameVirtualHost *:8086#保持绝对一致 <VirtualHost *:> ServerName localhost Serve ...
- Java 中无参带返回值方法的使用
如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...
- ipad iphone 开发的应用,加一个启动预览图片
如果你想在你的ipad,iphone应用启动是不显示黑屏幕,而是用一个图片代替的话,你只需要吧一张名为 Default.png 的图片加到项目里就行了. 很简单吧,新手们记得去这样美化下你的应用吗,很 ...