【HDU 5542】The Battle of Chibi (dp,树状数组优化)

目录

  • 题目
      • Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • Hint
      • Source
  • 思路
      • 朴素dp
      • 优化
  • 代码

题目

Description

Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao’s army. But all generals and soldiers of Cao Cao were loyal, it’s impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N N N information to be leaked, in happening order. Each of the information was estimated to has a i a_i ai value in Cao Cao’s opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M M M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N N N information and just select M M M of them. Find out how many ways Gai Huang could do this.

Input

The first line of the input gives the number of test cases, T    ( 1 ≤ 100 ) T\;(1≤100) T(1100). T T T test cases follow.

Each test case begins with two numbers N    ( 1 ≤ N ≤ 1 0 3 ) N\;(1≤N≤10^3) N(1N103) and M    ( 1 ≤ M ≤ N ) M\;(1≤M≤N) M(1MN), indicating the number of information and number of information Gai Huang will select. Then N N N numbers in a line, the i t h i_{th} ith number a i ( 1 ≤ a i ≤ 1 0 9 ) a_i(1≤a_i≤10^9) ai(1ai109) indicates the value in Cao Cao’s opinion of the ith information in happening order.

Output

For each test case, output one line containing Case #x: y, where x x x is the test case number (starting from 1 1 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007 ( 1 0 9 + 7 ) 1000000007(10^9+7) 1000000007(109+7).

Sample Input

2
3 2
1 2 3
3 2
3 2 1

Sample Output

Case #1: 3
Case #2: 0

Hint

In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order.
In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.

Source

The 2015 China Collegiate Programming Contest

思路

  • 主要题意:问你序列中长度为k的上升子序列有多少个。
  • 肯定是要用 可爱的 d p dp dp 做。
  • 朴素 d p dp dp 公式并不难想,可是时间复杂度太大,于是考虑优化。
  • 于是我们用树状数组优化 d p dp dp

朴素dp

应该很 e a s y   ( h a r d )   easy\ \sout{(hard)}\: easy (hard)吧。
我们假设 d p [ i ] [ j ] dp[i][j] dp[i][j] 表示长度为 j j j ,以 a [ i ] a[i] a[i] 为结尾的上升子序列的数量。
那么我们的状态转移方程如下:
d p [ i ] [ j ] = ∑ k = 0 j d p [ i − 1 ] [ k ] dp[i][j] = \sum\limits_{k = 0}^{j}dp[i-1][k] dp[i][j]=k=0jdp[i1][k] , 其中 a [ k ] < a [ j ] a[k] < a[j] a[k]<a[j]

于是代码就很 e a s y   ( h a r d )   easy\ \sout{(hard)}\: easy (hard)地打出来了

#include 

using namespace std;
const int M = 1000000007;
int a[2010], dp[1100][1100];

int main() {
	int T, cas = 0; scanf("%d", &T);
	while (T--) {
		int n, m, ans = 0; scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		memset(dp, 0, sizeof(dp));
		a[0] = -1292371547;
		dp[0][0] = 1;
		for (int i = 1; i <= m; i++)
		    for (int j = 1; j <= n; j++)
		        for (int k = 0; k < j; k++)
		            if (a[k] < a[j]) dp[i][j] = (dp[i][j] + dp[i-1][k]) % M;
		for (int i = 1; i <= n; i++) ans = (ans + dp[m][i]) % M;
		printf("Case #%d: %d\n", ++cas, ans);
	}
	return 0;
} 

(正解,确信)
【HDU 5542】The Battle of Chibi (dp,树状数组优化)_第1张图片
(作者,你这个三层for循环的正解挺牛啊!)

嘿嘿嘿, O ( n 2 m ) O(n^2m) O(n2m) 可还行 。
(反正有分)

优化

现在我们要考虑怎么对它进行优化。
首先,受状态限制,前两层是肯定没法再优化的了。
我们发现,做决策的时候用了 O ( n ) O(n) O(n)的时间复杂度,考虑对它优化。

具体怎么做呢?
我们注意:每当 j j j 变成 j + 1 j+1 j+1的时候,第三层 f o r for for 循环的 k k k 0 ∼ j − 1 0\sim j-1 0j1 变成了 0 ∼ j 0\sim j 0j ,也就是说,我们只多增加了 j j j 这个决策
那么我们需要维护一个决策集合,这个决策:

  • 可以插入一个新决策集合,以 a [ j ] a[j] a[j] 为关键码, d p [ i − 1 ] [ j ] dp[i-1][j] dp[i1][j] 为权值
  • 给定一个 a [ j ] a[j] a[j] ,查询满足 a [ k ] < a [ j ] a[k]a[k]<a[j] 的对应 d p [ i − 1 ] [ k ] dp[i-1][k] dp[i1][k] 的和。

于是,大家应该知道用哪个数据结构了吧。
——树状数组

(线段树屁颠颠地跑了过来,又灰溜溜的回去了)
(确认过眼神,是被卡常的人……)


简单讲一下树状数组。
【HDU 5542】The Battle of Chibi (dp,树状数组优化)_第2张图片
树状数组是一种用于维护前缀信息的的数据结构,查询和修改的时间复杂度都是 O ( l o g    n ) O(log\;n) O(logn) ,主要用于查询任意两位之间所有元素之和。

树状数组相比线段树系数要少很多,比传统数组要快,而且更易写。
但其实刚刚被卡常回家的线段树更好用一些树状数组只能算是线段树的小弟。
一定要知道:树状数组能解决的问题线段树一定能解决,而线段树能解决的问题树状数组却不一定能解决。


(^o^)/~ 用树状数组来支持这样的一个集合是完全OK的。
可是 a [ i ] a[i] a[i] 太大了,需要离散化。
我们需要将 a [ i ] a[i] a[i] 映射到 1 ∼ n + 1 1\sim n+1 1n+1上来。
相视一笑,来算复杂度
【HDU 5542】The Battle of Chibi (dp,树状数组优化)_第3张图片
总时间复杂度 O ( n m    l o g   n ) O(nm\;log\:n) O(nmlogn)
P e r f e c t Perfect Perfect

OK, nothing to bb anymore.
Show you the Code ↓↓↓

在牛客和acwing 都能过,在hdu超时。
神奇了。求解释。

代码

#include 
#include 
#include 
#include 

using namespace std;
const int M = 1000000007;
int a[2010], dp[1100][1100], b[2010], c[2010], n;

inline int lowbit(int x) {return x & (-x);}

int sum(int x) {
	int ret = 0;
	while (x) {
		ret = (ret+c[x]) % M;
		x -= lowbit(x);
	}
	return ret;
}

void add(int x, int val) {
	while (x <= n) {
		c[x] = (c[x]+val) % M;
		x += lowbit(x);
	}
	return ;
}

int main() {
	int T, cas = 0; scanf("%d", &T);
	while (T--) {
		int m, ans = 0; scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
		sort(b+1, b+n+1);
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 1; i <= m; i++) {
			memset(c, 0, sizeof(c));
			add(1, dp[i-1][0]);
		    for (int j = 1; j <= n; j++) {
		    	dp[i][j] = sum(lower_bound(b+1, b+n+1, a[j])-b);
		    	add(lower_bound(b+1, b+n+1, a[j])-b+1, dp[i-1][j]);
			}
		} 
	 for (int i = 1; i <= n; i++) ans = (ans + dp[m][i]) % M;
		printf("Case #%d: %d\n", ++cas, ans);
	}
	return 0;
} 

你可能感兴趣的:(dp,#,优化dp)