题目链接: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,树状数组)的更多相关文章

  1. 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 ...

  2. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

  3. 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 ...

  4. hdu5542 The Battle of Chibi【树状数组】【离散化】

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  5. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  6. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  7. 【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 ...

  8. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  9. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

随机推荐

  1. SqlServer:此数据库处于单用户模式,导致数据库无法删除的处理

    此数据库处理单用户模式,尚在连接当中,无法删除(既使将SQLServer停止后再启动也是如此) USE [master] GO /****** Object: StoredProcedure [dbo ...

  2. an'gularjs 环境搭建之NodeJS、NPM安装配置步骤(windows版本)

    NodeJS.NPM安装配置步骤(windows版本)  :http://xiaoyaojones.blog.163.com/blog/static/28370125201351501113581/ ...

  3. TI BLE CC2541的SPI主模式

    SPI就是用4条线来串行传输数据, 2541只能用模拟的方式用GPIO来做. //*********************************************************** ...

  4. HTML5与移动端Web

    概述 HTML5 提供了很多新的功能,主要有: 新的 HTML 元素,例如 section, nav, header, footer, article 等 用于绘画的 Canvas 元素 用于多媒体播 ...

  5. 【原创】纯干货,Spring-data-jpa详解,全方位介绍。

    本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...

  6. Apache httpd和JBoss构建高可用集群环境

    1. 前言 集群是指把不同的服务器集中在一起,组成一个服务器集合,这个集合给客户端提供一个虚拟的平台,使客户端在不知道服务器集合结构的情况下对这一服务器集合进行部署应用.获取服务等操作.集群是企业应用 ...

  7. wordpress网站被挂马以及防御方法

    wordpress本身的安全性是非常的高的,一般不会被轻易的破解,被挂马,但是我们也不能够过度迷信wordpress的安全性,凡是连接上互联网的服务器和电脑,都存在被破解的风险性.所以我们在日常维护自 ...

  8. PHP和Java的主要区别有哪些呢?

    解释 PHP与Java作为两种截然不同的程序开发语言,无论是技术层面还是发展空间,亦或是市场份额占比,都有着不同的表现方式,理念上的不同导致了Java和PHP在Web应用开发上显示了不同的结果.若要一 ...

  9. 使用PHP flush 函数的时候我们需要注意些什么呢?

    WebServer(可以认为特指apache)的缓冲区.在apache module的sapi下, flush会通过调用sapi_module的flush成员函数指针,间接的调用apache的api: ...

  10. Java 使用 Redis | 菜鸟教程

    入门教程: http://www.runoob.com/redis/redis-java.html 中文手册: http://redis.readthedocs.io/en/2.4/index.htm ...