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 ...
随机推荐
- HDU 1087 Super Jumping! Jumping! Jumping! --- DP入门之最大递增子序列
DP基础题 DP[i]表示以a[i]结尾所能得到的最大值 但是a[n-1]不一定是整个序列能得到的最大值 #include <bits/stdc++.h> using namespace ...
- July 3rd, Week 28th Sunday, 2016
2016-07-03 First-hand experience is often necessary for personal growth. 亲身体验通常是成长所必须的. Although som ...
- apache ab下载测试
http://httpd.apache.org/docs/2.0/programs/ab.html-->http://httpd.apache.org/docs/current/platform ...
- 转载_虚拟机下LInux(终端)配置网络的方法
出自: http://www.360doc.com/content/14/1027/11/17496895_420258403.shtml 对文章的重点进行剪贴,方便查看. 这几天在虚拟机vmware ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(零) 前言
前端时间听一个技术朋友说 LayIM 2.0 发布了,听到这个消息抓紧去官网看了一下.(http://layim.layui.com/)哎呀呀,还要购买授权[大家支持一下哦],果断买了企业版,喜欢钻研 ...
- iOS获取状态栏和导航栏尺寸(宽度和高度)
iPhone开发当中,有时需要获取状态栏和导航栏高度.宽度信息,方便布局其他控件.下面介绍一下如何获取这些信息: // 状态栏(statusbar) CGRect rectStatus = [[UIA ...
- NYOJ之素数求和问题
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAKCCAIAAABH/2gWAAAgAElEQVR4nO3dPVLjStsG4G8T5CyEdF
- python中random模块使用
- AJAX XML返回类型
例题 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- Linux USB Project
转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...