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 ...
随机推荐
- 【python】词法语法解析模块ply
官方手册:http://www.dabeaz.com/ply/ply.html 以下例子都来自官方手册: 以四则运算为例: x = 3 + 42 * (s - t) 词法分析: 需要将其分解为: 'x ...
- 调用c++接口类
调用c++接口类 public class CarDeviceDll { /*对dll库进行一些初始化*/ [DllImport("IDI.dll")] public static ...
- Meta标签实现阻止移动设备(手机、Pad)的浏览器双击放大网页
一.背景 在当今这个移动设备发展越来越快,并且技术越来越成熟的时代,移动设备成了企业扩展业务不可或缺的重要领域之一,随之而来的是适应手机的网站层出不穷,在开发过程中,我们往往会遇到一个很尴尬的问题:移 ...
- 让chrome的控制台更高大上(装B用)
作为一名屌丝前端程序员,在装B的道路上是越走越远了!废话不多说,直接先上几张图:我的Element是这样的: 我的console是这个样子的: 我的Timeline是这个样子的: ---------- ...
- CUDA中Bank conflict冲突
转自:http://blog.csdn.net/smsmn/article/details/6336060 其实这两天一直不知道什么叫bank conflict冲突,这两天因为要看那个矩阵转置优化的问 ...
- css3圣诞雪景球开源
css3圣诞雪景球开源 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- main方法并发测试
public static void main(String[] args) throws Exception{ RequestModel r = new RequestModel(); r.setT ...
- 中文和unicode互转
public class Test { public static void main(String[] args) { String uname="欧阳红"; for (int ...
- 在R语言中无法设置CRAN镜像问题
很大的可能是因为使用的浏览器不是IE浏览器的问题,因为CRAN的镜像需要用IE浏览器来打开. 只需要按照下面设置即可: 1.打开IE-->设置-->Internet选项-->高级 2 ...
- http://poj.org/problem?id=3278(bfs)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 76935 Accepted: 24323 ...