DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
题意:问n长度的序列,找出长度m的上升子序列的方案数。
分析:这个问题就是问:dp[i][j] = sum (dp[i-1][k]) (1 <= k <= n, a[k] < a[j]),当前长度i一层,最后下标j一层,之前的最后下标k一层,这样是n^3的复杂度。然后树状数组可以优化j,k的复杂度,就是j扫描一遍的同时,将a[j]的信息更新到树上,那么扫描就可以用 logn时间统计出k的信息,在这之前先对a[]离散化。
/************************************************
* Author :Running_Time
* Created Time :2015/10/21 星期三 13:20:36
* File Name :C.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
struct BIT {
int c[N], SZ;
void add(int &a, int b) {
a += b;
while (a >= MOD) a -= MOD;
}
void init(int n) {
memset (c, 0, sizeof (c));
SZ = n;
}
void updata(int i, int x) {
while (i <= SZ) {
add (c[i], x); i += i & (-i);
}
}
int query(int i) {
int ret = 0;
while (i) {
add (ret, c[i]); i -= i & (-i);
}
return ret;
}
}bit;
int a[N], A[N];
int dp[N][N]; void compress(int n) {
sort (A, A+n);
int nn = unique (A, A+n) - A;
for (int i=0; i<n; ++i) {
a[i] = lower_bound (A, A+n, a[i]) - A + 1;
}
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
int n, m; scanf ("%d%d", &n, &m);
bit.init (n);
for (int i=0; i<n; ++i) {
scanf ("%d", &a[i]);
A[i] = a[i];
}
compress (n);
memset (dp, 0, sizeof (dp));
for (int i=0; i<n; ++i) dp[1][i] = 1;
int ans = 0;
for (int i=2; i<=m; ++i) {
bit.init (n);
for (int j=0; j<n; ++j) {
dp[i][j] = bit.query (a[j] - 1);
bit.updata (a[j], dp[i-1][j]);
}
}
for (int i=0; i<n; ++i) bit.add(ans, dp[m][i]);
printf ("Case #%d: %d\n", ++cas, ans);
} return 0;
}
DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi的更多相关文章
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- UESTC 1217 The Battle of Chibi
dp+树状数组优化. dp[i][j]表示以a[i]结尾,最长上升序列长度为j的方案数.dp[i][j]=sum{dp[k][j-1]} 其中k<i&&a[k]<a[i]. ...
- CDOJ 1217 The Battle of Chibi
The Battle of Chibi Time Limit: 6000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- 【BZOJ-1010】玩具装箱toy DP + 斜率优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8432 Solved: 3338[Submit][St ...
- DP及其优化
常见DP模型及其构造 序列DP ARC074 RGB Sequence 题意 给你一个长度为 \(n\) 的序列和 \(m\) 组约束条件,每组条件形如 \(l_i,r_i,x_i\),表示序列上的 ...
- 动态规划DP的优化
写一写要讲什么免得忘记了.DP的优化. 大概围绕着"是什么","有什么用","怎么用"三个方面讲. 主要是<算法竞赛入门经典>里 ...
- 「学习笔记」wqs二分/dp凸优化
[学习笔记]wqs二分/DP凸优化 从一个经典问题谈起: 有一个长度为 \(n\) 的序列 \(a\),要求找出恰好 \(k\) 个不相交的连续子序列,使得这 \(k\) 个序列的和最大 \(1 \l ...
- [bzoj1044][HAOI2008][木棍分割] (二分+贪心+dp+队列优化)
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
随机推荐
- [POJ1003]Hangover
[POJ1003]Hangover 试题描述 How far can you make a stack of cards overhang a table? If you have one card, ...
- 基于DCMTK的DICOM相关程序编写攻略
2008年09月10日 星期三 15:35 基于DCMTK的DICOM相关程序编写攻略 前言: 由于现在的医学影像设备的图像存储和传输正在逐渐向DICOM标准靠拢,在我们进行医学图像处理的过程中,经常 ...
- (转)搞ACM的你伤不起
劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资讲算法是程序设计的核心啊!!!!!! 尼玛除了面试题就没见过用算法的地方啊!!!!!! 谁再跟 ...
- Mac 下 Chrome 浏览器 快捷键
⌘-Option-I 打开“开发人员工具”. ⌘-Option-J 打开“JavaScript 控制台”. ⌘-Option-U 打开当前网页的源代码. 转自: http://www.harbin-s ...
- 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”
VS2005下,添加了母版页这个控件,我们可以讲N个页面中共同的部分放在母版页来实现,并让WEB窗体集成自我们的母版页,就可以让我们的站点具有统一的风格了.在VS2005SP1之前的版本中,我们只能创 ...
- Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 反弹SHELL
[姿势] http://www.91ri.org/6620.html http://www.waitalone.cn/linux-shell-rebound-under-way.html [图释] h ...
- MinGW/MSYS 交叉编译环境搭建
因为包的依赖关系不清楚,搭建时出错也不知道是什么原因,下面链接老外写的搭建步骤,写的非常详细还有脚本 已经编译的下载地址 http://ingar.satgnu.net/devenv/mingw32/ ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- DP:Corn Fields(POJ 3254)
北大教你如何高效养牛(误)(点我查看) 2015-08-21: 问题的大意就是有一片稻田,里面有很多坑,你要在上面种稻谷,然后呢田里面还会养牛,牛不喜欢扎堆吃饭,所以呢你种的稻谷要间隔种在坑里面,所 ...