ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案
dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数
那么最终的答案应该就是sigma(dp[i][m]);
显然有:dp[i][j] = sigma(dp[k][j - 1]); 其中 1 <= k < i 且 a[k] < a[i];
题目要求严格递增,这个限制怎么解决?
hdu4719这道题同样是这样处理,即我们只需要从小到大dp就行了。
但是复杂度是O(n^3)的,显然需要优化,注意到应该从小到大dp之后,我们要做的就是快速求出sigma(dp[k][j-1]) (还未计算到的dp值为0) (注意不能直接用数组维护前缀和)
可以用树状数组得到优化,最终复杂度是O(n^2logn)
#include <bits/stdc++.h>
#define lowbit(x) (x) & (-x)
using namespace std; const int N = 1005;
const int M = 1e9 + 7; int dp[N][N], c[N][N];
int a[N], r[N]; bool cmp(int b, int c) {
return a[b] < a[c];
} void update(int i, int j, int value)
{
while(i < N) {
c[i][j] = c[i][j] + value % M;
i += lowbit(i);
}
}
int sum(int i, int j)
{
int s = 0;
while(i > 0) {
s = s + c[i][j] % M;
i -= lowbit(i);
}
return s % M;
}
int main()
{
int n, m;
int _, cas = 1; scanf("%d", &_);
while(_ --)
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
memset(c, 0, sizeof c);
memset(dp, 0, sizeof dp);
for(int i = 0; i <= n; ++i) r[i] = i;
sort(r + 1, r + n + 1, cmp); for(int i = 1; i <= n; ++i) {
int id = r[i];
dp[id][1] = 1;
update(id, 1, 1);
for(int j = 2; j <= m; ++j) {
dp[id][j] = sum(id - 1, j - 1);
update(id, j, dp[id][j]);
}
} int ans = 0;
for(int i = 1; i <= n; ++i) ans = ans + dp[i][m] % M;
printf("Case #%d: %d\n", cas++, ans % M);
}
return 0;
}
ccpc_南阳 C 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 ...
- 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 ...
随机推荐
- java获取客服端信息(系统,浏览器等)
String agent = request.getHeader("user-agent"); System.out.println(agent); StringTokenizer ...
- ajax+bootstrap做弹窗
建页面,引入bootstrap弹窗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- [Android Pro] AOSP download
Ubuntu14.04系统下载Android源码,直接上步骤: 清华大学 TUNA 镜像源 https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ https: ...
- No space left on device you must specify the filesystem type--Linux重启挂在失败
在Linux中拷贝了一个文件比较大5G,直接提示:No SPace Left On Device,很明显是磁盘空间不够了,我因为是在虚拟机上面建的,直接右击虚拟机==>编辑设置 如图片1所示, ...
- Clr Via C#读书笔记---CLR寄宿和应用程序域
#1 CLR寄宿: 开发CLR时,Microsoft实际是将他实现成包含在一个dll中的COM服务器.Microsoft为CLR定义了一个标准的COM接口,并为该接口和COM服务器分配了GUID.安装 ...
- Tensorflow 的Word2vec demo解析
简单demo的代码路径在tensorflow\tensorflow\g3doc\tutorials\word2vec\word2vec_basic.py Sikp gram方式的model思路 htt ...
- dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get
在64位的操作系统安装oracle10g 软件安装完成后,使用dbca建库的时候报下面的错: $ dbcaUnsatisfiedLinkError exception loading native l ...
- golang time and duration
package mainimport "fmt"import "time"func main() { p := fmt.Println // We'll sta ...
- html5 web database
html5 web database <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- C# DatrgridView表格控件的一些用法
public class useDatrgrivView { string conn = null; string sqlComm = null; DataSet das = null; DataGr ...