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 ...
随机推荐
- objective-c可变字典
1 #pragma mark *****************************字典******************************** 2 // 字典:通过ke ...
- IIS 发布mvc 403.14
转载: iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for “IIS APPPOOL\ASP ...
- [Android Pro] Android Fragment getActivity返回null解决
overide FragmentActivity onSaveInstanceState method like this. @Override public void onSaveInstance ...
- mongodb3.x用户角色
用户和角色是多对多的关系,一个用户可以对应多个角色,一个角色可以拥有多个用户.用户角色的不同对应的权限也是不一样的.下面是一些分配给用户的常见的角色. read ...
- 数对的个数(cogs610)
Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A- ...
- AJAX 搜索自动显示练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mysqlbinlog 查看日志时发生报错
[root@cs Downloads]# mysqlbinlog mysql-bin. ERROR: Error , event_type: ERROR: Could not read entry a ...
- C/C++学习笔记----指针的理解
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...
- Linux 配置NFS,文件共享
配置: 1.设定共享主机服务器 ---(注意防火墙) 编辑ipA端的/etc/exports 文件 [root@dbrac2 ~]# cat /etc/exports /media 192 ...
- 算法系列:Reservoir Sampling
copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...