hdu 5542 The Battle of Chibi(2015CCPC - C题)
题目链接:hdu 5542
首届CCPC的C题,比赛时一起搞了好久,最后是队友A出的,当时有试过用树状数组来优化 dp,然后今天下午也用树状数组搞了一下午,结果还是踩了和当时一样的坑:我总是把用来记录状态的 dp 数组和树状数组里的内置数组混在一起使用了,而且两重循环的顺序也反了,以至于两组数据
3 2 3 2
1 2 3 和 3 2 1
程序跑都得出了相同的结果,无语。。。之前做 dp 和线段树结合的题时也是傻傻地分不清,说到底就是 dp 的功力很不够,所以在 dp 专场的这场比赛中除了水题外我几乎没什么贡献了,很心塞郁闷了一段时间
题目思路:先建立一个经典的 dp 模型:f[k][i] 表示长度为 k,以 b[i] 结尾的上升子序列的数量(即 dp 的状态),所以递推方程为 f[k][i] = sum{ f[k - 1][x], 1<=x< i && 1<= b[x] <= b[i]-1 },可以看到 k 只依赖于 k-1,而 i 依赖于 b[i] 前面并比 b[i] 小的(和经典的逆序数原理一样),所以可以用树状数组来快速求出,之所以写成 f[k][i] 而不是 f[i][k] 是为了方便树状数组的操作(其实调过来完全可以的,不过脑子一时间转不过弯来,树状数组一直以来都写得太死了,总是模板大法 -_-||)程序实现中还充斥着各种技巧等。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lowbit(x) ((x) & -(x))
typedef long long ll;
const int N = ;
const ll mod = ; int maxn;
ll c[N][N]; // c 数组和 maxn 是树状数组所需的数据结构
ll f[N][N]; // f[k][i] 表示长度为 k,以 b[i] 结尾的上升子序列的数量(即 dp 的状态) ll sum(int len, int x) { // 树状数组的两个函数
ll res = ;
while(x) {
res += c[len][x]; // c[][] 的 len 对应 f[][] 的 k,x 对应 f[][] 的 b[i]
if(res >= mod) res -= mod;
x -= lowbit(x);
}
return res;
} void add(int len, int x, ll v) {
while(x <= maxn) {
c[len][x] += v;
if(c[len][x] >= mod) c[len][x] -= mod;
x += lowbit(x);
}
} int b[N], a[N]; int main() {
int t,n,m,Case = ;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i = ; i <= n; ++i) {
scanf("%d", b + i);
a[i] = b[i];
}
sort(a + , a + n + ); // 离散化
for(int i = ; i <= n; ++i)
b[i] = lower_bound(a + , a + n + , b[i]) - a; maxn = n + ;
memset(c, , sizeof c);
memset(f, , sizeof f); // 记得都要清零 ll ans = ;
// 两重循环的顺序不能搞错!
// 每次对于每个 b[i],都是已经算好了长度 1~min(i,m) 的上升子序列的数量,即 f[1~min(i,m)][i] 的值;
// 然后再到 b[i + 1];所以是先枚举 1~n 的离散化后各个元素,再枚举长度(1~min(i,m))
for(int i = ; i <= n; ++i) {
int top = min(i,m);
for(int k = ; k <= top; ++k) {
if(k == ) f[k][i] = ;
else {
f[k][i] += sum(k - , b[i] - ); // f[k][i] = sum{f[k - 1][x], 1 <= b[x] <= b[i]-1 }
if(f[k][i] >= mod) f[k][i] -= mod;
}
add(k, b[i], f[k][i]); // 把算好的 f[k][i] 更新进树状数组中去
}
ans += f[m][i]; // 这时候 f[m][i] 已经算好了,所以加入到答案中
if(ans >= mod) ans -= mod;
}
printf("Case #%d: %I64d\n", ++Case, ans);
}
return ;
}
二重循环中,先固定 i,然后枚举 k,颠倒过来是不行的,原因是如果先固定 k 而枚举 i 的话那么计算完 k-1 后,1~n 的元素都已经进入树状数组中去了,也就是在计算 i 时,i 后面的元素都已经进入树状数组中,这样子就混淆了之前的元素,就好像求逆序数时必须从前往后按顺序来,否则后面比前面的先插入到树状数组中就没意义了。
dp 数组记录状态,然后更新进相应的数据结构中,和 hdu 4521 小明系列问题——小明序列 有相似之处,必须好好体会下 dp 和数据结构结合的这种思想与处理方式。
hdu 5542 The Battle of Chibi(2015CCPC - C题)的更多相关文章
- 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 ...
- 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 ...
- 【树状数组+dp】HDU 5542 The Battle of Chibi
http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结 ...
- [HDU 5542] The Battle of Chibi
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...
- HDOJ 5542 The Battle of Chibi
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题目大意:在n个数中找长度为m的单调上升子序列有多少种方案 题目思路:DP,离散化,树状数组优化 ...
- The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 2015南阳CCPC C - The Battle of Chibi DP
C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- CDOJ 1217 The Battle of Chibi
The Battle of Chibi Time Limit: 6000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
随机推荐
- Wordpress基础:文章和页面的区别
页面: 页面是你可以单独建立一个固定页面,可以作为留言板,或者通知的单页面,发布之后是固定的网址. 页面并不能被分类.亦不能拥有标签,但是它们可以有层级关系.您可将页面附属在另一个页面之下. 对应模板 ...
- 采用CSS3的动态元素(动画)设计div块的层级式展现
此练习作品是为学习HTML5+CSS3而设计的(如有不好请大家批评指教~~~). 操作:当页面加载时,点击网页中的绿色块(一层),则有其他几块(二层)从其中央出现并向外延伸并旋转,点击这几块中任意一个 ...
- discuz安装
1.upload文件复制到根目录下,访问,安装 2.数据库需要提前建好 3.数据库地址默认是localhost,我安装时是127.0.0.1 4.最好在本地安装时,配置本地域名和线上域名一样,不然迁移 ...
- Python开发【第三章】:Python函数介绍
一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...
- bbs网站 models
bbs网站 models #!/usr/bin/env python #_*_coding:utf-8_*_ from django.db import models from django.cont ...
- KinderEditor编辑器使用
KinderEditor编辑器的使用 分为简单的三步.1:添加引用部分 <script src="/KinderEditor/kindeditor-min.js">&l ...
- Android实现apk文件下载并自动安装
//下载apk程序代码 protected File downLoadFile(String httpUrl) { // TODO Auto-generated method stub final S ...
- Mysql乱码
MySql字符集 1.系统默认的.数据库默认的.表格默认的.列的 真正决定权在列定义上 2.latin1 系统默认字符编码 字符范围是0x00-0xff,可以存放任意编码的字符序列. 3.utf8编码 ...
- [译]JavaScript规范-葵花宝典
[译]JavaScript规范 译自:https://github.com/airbnb/javascript 类型 原始值: 相当于传值 string number boolean null und ...
- javascript设计模式简介