题意:给你一个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 + 树状数组的更多相关文章

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

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

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

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

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

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

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

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

  7. 奶牛抗议 DP 树状数组

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

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

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

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

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

随机推荐

  1. 【leetcode】Reverse Words in a String(hard)☆

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  2. linux安装软件

    安装方式一: RPM包安装 安装方式二:yum包安装 安装方式三:源码包安装 安装方式四:脚步安装包 视频教程

  3. chaper3_exercise_Uva1585_score

    #include<iostream> #include<string> using namespace std; int main(void) { , j = ; string ...

  4. SOCKet 编程 简介

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  5. 一个简单的jsp+servlet实例,实现简单的登录

    开发环境myeclipse+tomcat6 1.先创建web project,项目名为RegisterSystem, 2.在WebRoot 目录下创建login.jsp文件,只需修改body中的内容, ...

  6. highcharts的简单使用

    在使用过的图表js插件中,个人认为还是highcharts最好,无论从兼容性,渲染速度,甚至是文档详细上来说,都一直觉得highcharts更胜一筹.现在花点时间做一下简单的总结,比如从一个矩形图开始 ...

  7. JDBC 精度

    http://www.cnblogs.com/tobecrazy/p/3390021.html http://www.cnblogs.com/kerrycode/p/4034231.html http ...

  8. SQLServer多表连接查询

    双表内部连接查询 select wName,dName from DepartMent,Worker where DepartMent.dID=Worker.did select wName,dNam ...

  9. SVN 升级后出现You need to upgrade the working copy first.

    今天将svn更新后,出现 svn: The working copy at 'E:\591woospace\kst_fashion_alipay_v1.2.0\src\com\kstapp\wansh ...

  10. 使用Delphi对象(声明、实例化、构造、释放)

    一.声明和实例化 在使用一个对象之前,用class关键字声明一个对象.可以在一个程序或单元的type部分声明一个对象类型: type TFooObject = class; 除了声明一个对象类型,通常 ...