[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 ...
随机推荐
- jquery冲突细节
使用jquery报一个错误,之前也遇到过,今天记录下来,方便以后使用 Uncaught TypeError: Object #<Object> has no method 'test' 这 ...
- MYSQL 、Oracle、SQLServer 数据库中时间的格式化输出
在MYSQL 中格式化输出 date_forma t(date,'yyyyMMddHHmmss') Oracle 中格式化输出 to_char(time ,'yyyyMMddHHmmss') SQL ...
- Delphi的DateToStr StrToDate格式灵活用法
Delphi的DateToStr StrToDate格式灵活用法 2008-04-09 10:19 procedure TForm1.Button1Click(Sender: TObject);var ...
- javaWeb 使用cookie显示上次访问网站时间
package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date ...
- linux源码Makefile详解(完整)【转】
转自:http://www.cnblogs.com/Daniel-G/p/3286614.html 随着 Linux 操作系统的广泛应用,特别是 Linux 在嵌入式领域的发展,越来越多的人开始投身到 ...
- [thml]HTML select标签 获取选中的option的value及Text内容
很简单的select标签: <select id="hello" onchange="getContent(this.value,this.options[this ...
- sql 取时间 问题集
一. AND B.TRAFFICE_DATE>dateadd(day,5,(select getdate())) dateadd(day,5,(select getdate())):为当前时间+ ...
- Excel 函数
Excel 函数: 一.定义: Excel 函数即是预先定义,执行计算.分析等处理数据任务的特殊公式. 二.结构: 1.单一结构 =函数名(参数1,参数2,参数3.....) 示例:=sum(A3:A ...
- MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作
MVC模式(Model View Controller): Model:DAO模型 View:JSP 在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...
- U3D UGUI学习3 - RectTransform
总的来说整合了NGUI很多零散功能,比如NGUI2.X处理拉伸要额外套脚本,NGUI3.X开始引入新的锚点.再加上依赖BoxCollider使得整个HUD显示非常乱 而UGUI很清晰明了,你也能看清楚 ...