[HDOJ5542]The Battle of Chibi(DP,树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542
题意:n个数中找m个数,使得从左到右读是上升的子序列。问一共有多少种。
dp(i,j)表示取到第i个位置,长为j并且最后一个数为a(i)的方案总数。
更新就比较容易了,dp(i,j)=∑(k=1->j-1)dp(i-1,k),初始化dp(i,1)=1。
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) x & (-x) const int mod = int(1e9+);
const int maxn = ;
int dp[maxn][maxn];
int a[maxn], h[maxn];
int n, m, hcnt; int sum(int i, int x) {
int ret = ;
while(x) {
ret = (ret + dp[i][x]) % mod;
x -= lowbit(x);
}
return ret;
} void update(int i, int x, int k) {
while(x <= n) {
dp[i][x] = (dp[i][x] + k) % mod;
x += lowbit(x);
}
} int getid(int x) {
return lower_bound(h, h+hcnt, x) - h;
} int main() {
//freopen("in", "r", stdin);
int T, _ = ;
scanf("%d", &T);
while(T--) {
printf("Case #%d: ", _++);
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
h[i-] = a[i];
}
sort(h, h+n); hcnt = unique(h, h+n) - h;
for(int i = ; i <= n; i++) a[i] = getid(a[i]) + ;
memset(dp, , sizeof(dp));
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(j == ) {
update(j, a[i], );
continue;
}
int tmp = sum(j-, a[i]-);
update(j, a[i], tmp);
}
}
printf("%d\n", sum(m, n));
}
return ;
}
[HDOJ5542]The Battle of Chibi(DP,树状数组)的更多相关文章
- 2015南阳CCPC C - The Battle of Chibi DP树状数组优化
C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- 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 ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
随机推荐
- Report launcher to run SSRS report subscriptions on demand
http://www.mssqltips.com/sqlservertip/3078/report-launcher-to-run-ssrs-report-subscriptions-on-deman ...
- Windows7(x64)下Oracle10g安装
安装环境:Windows7 (64位版本) + Oracle10g 问题描述1:无法启动安装程序,程序提示“程序异常终止.发生内部错误....” 解决过程:按网上说法加6.1版本参数,按xp兼容模式启 ...
- 代码坏味道特别篇————Long parameter List 过长的参数列表
刚开始学习编程时,老师说:讲方法所需要的东西都以参数的形式传入,那是我们好像还没学OO这个东东,要不就弄成全局变量,我擦,全局变量可牛逼了,刚开始学习的时候我们都在用全局变量,可是后来工作了,经理说不 ...
- C#中Attribute的继承
在C#中Attribute是个非常有用的语法,本文不会介绍Attribute的使用方法,如果想了解Attribute的详细信息请查阅MSDN及网上相关文档.C#中的Attribute有两个地方是和继承 ...
- kvm虚拟机virt-manager启动报错
安装kvm,用virt-manager启动时报错如下: Traceback (most recent call last): File "/usr/share/virt-manager/v ...
- javascript,jquery代码规范
jquery代码规范 Coding Standards & Best Practices 中文翻译版:jquery编程的标准写法和最佳实践 javascript代码规范 JavaScript编 ...
- telnet不通11211端口,防火墙
问题描述: 按照官网步骤,虚拟机里安装并启动memcached, 虚拟机里自己telnet11211端口可以连接, 使用Xmanager22端口可以连接到虚拟机,但是始终telnet不同11211端口 ...
- Intellij IDEA
http://1358440610-qq-com.iteye.com/blog/2102195
- jar包里查找指定的class文件,排查是否存在或重复,工具软件:Java Class Finder
jar包里查找指定的class文件,排查是否存在或重复,工具软件:Java Class Finder 1,下载工具地址:www.idesksoft.com/classfinder.html,如图: 2 ...
- MySQL Replication浅析
MySQL Replication是MySQL非常出色的一个功能,该功能将一个MySQL实例中的数据复制到另一个MySQL实例中.整个过程是异步进行的,但由于其高效的性能设计,复制的延时非常小.MyS ...