HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542

Problem 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 information to be leaked, in happening order. Each of the information was estimated to has 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 information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select 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 test cases follow.

Each test case begins with two numbers N(1≤N≤10^3) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤10^9) 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 is the test case number (starting from 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(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.

题意:

给出长度为 $N(1 \le N \le 10^3)$ 的数字序列(每个数字都在 $[1,10^9]$ 范围内)。

现在要从中挑选出长度为 $M(1 \le M \le N)$ 的严格单调递增子序列,问有多少种挑选方案。

题解:

首先,不难想到应当假设 $dp[i][j]$ 代表以 $a[i]$ 为结尾的且长度为 $j$ 的严格单增子序列的数目,

那么自然地,状态转移就是 $dp[i][j] = \sum dp[k][j-1]$,其中 $k$ 满足 $1 \le k < i$ 且 $a[k]<a[i]$。

不过,题目是不可能这么简单的……这样纯暴力dp的话时间复杂度为 $O(n^3)$,超时。

考虑进行优化:

先对 $dp$ 数组进行改造,假设 $dp[a_i][j]$ 代表:在数字序列的 $[1,i]$ 区间内,以数字 $a[i]$ 为结尾的长度为 $j$ 的严格单增子序列的数目,

这样一来,原来的 $a[i]$ 的范围在 $[1,10^9]$,数组是开不下的。所以,需要对 $a[1 \sim n]$ 先进行离散化,使得所有 $a[i]$ 的范围均在 $[1,n]$,这样数组就开的下了。

自然而然地,状态转移方程就变成了 $dp[a_i][j] = dp[1][j-1] + dp[2][j-1] + \cdots + dp[a_i-1][j-1]$。

然后,既然要优化求前缀和的速度,不妨对 $dp[1 \sim n][1]$ 构造一个树状数组,对 $dp[1 \sim n][2]$ 构造一个树状数组,$\cdots$,对 $dp[1 \sim n][m]$ 构造一个树状数组。

这样一来,我要求 $dp[1][j-1] + dp[2][j-1] + \cdots + dp[a_i-1][j-1]$ 这样一个前缀和就可以在 $O(\log n)$ 时间内完成。总时间复杂度变为 $O(n^2 \log n)$。

那么最后所求答案即为 $dp[1][m]+dp[2][m]+ \cdots + dp[n][m]$。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+5;
const ll mod=1e9+7;

int n,m;
int a[maxn];
ll dp[maxn][maxn];

vector<int> v;
inline int getid(int x) {
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}

inline int lowbit(int x){return x&(-x);}
void add(int pos,int len,ll val)
{
    val%=mod;
    while(pos<=n)
    {
        dp[pos][len]+=val;
        dp[pos][len]%=mod;
        pos+=lowbit(pos);
    }
}
ll ask(int pos,int len)
{
    ll res=0;
    while(pos>0)
    {
        res+=dp[pos][len];
        res%=mod;
        pos-=lowbit(pos);
    }
    return res;
}

int main()
{
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%d%d",&n,&m);

        v.clear();
        for(int i=1;i<=n;i++) scanf("%d",&a[i]), v.push_back(a[i]);
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        for(int i=1;i<=n;i++) a[i]=getid(a[i]);

        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(j==1) add(a[i],j,1);
                else
                {
                    ll tmp=ask(a[i]-1,j-1);
                    add(a[i],j,tmp);
                }
            }
        }
        printf("Case #%d: %lld\n",kase,ask(n,m));
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9898230.html

时间: 2024-10-12 02:29:50

HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]的相关文章

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_{i}}$的最小值. 首先二分答案,那么每个物品的权值就变成了$x * b_{i} - a_{i}$ 在判断的时候先把那些权值为正的物品全部选出来, 然后记录一下从$1$开始可以覆盖到的最右端点的位置. 接下来开始DP,按照区间的端点升序排序(左端点第一关键字,右端点第二关键字) 问题转化为能否用剩

LUOGU P2344 奶牛抗议 (树状数组优化dp)

传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j-1])$,但是这样的时间复杂度是$O(n^2)?$,所以考虑优化,发现必须满足$sum[i]\ge sum[j-1]?$才能进行转移,那么直接离散化后用树状数组维护一个前缀和即可. #include<iostream> #include<cstdio> #include<cstr

hdu 3450 树状数组优化dp

题意就不说了: hdu2227差不多只不过这道题不是递增  而是相邻差不超过d   其实是为都一样,  也有差别: 这道题没说范围  并且树之间的大小关系对结果有影响,所以树状数组里根据原来id来求,由于数值很大 所以还是用到离散化  这里的离散化感觉和映射差不多,离散化用来查找id: 当num[i]的id是x是,dp[i]表示方案数  很明显dp[i]=sun(dp[j],j<i&&abs(num[j]-num[i])<=d)  这里在树状数组里面就是求sum[1到num[i

POJ 5542 树状数组优化DP

题意:给长度为n的数组,问有多少长度为m单调递增子序列? n,m<=1000 思路:设f[i][j]表示长度为i的以aj为结尾的单调递增子序列的方案数,易得f[i][j]=f[i][j]+f[i-1][k] (ak<aj) 第一层枚举n,第二层枚举m,第三层枚举小于m的位置,其中第一层,第二层由于状态方程是无法改变的,而第三层枚举小于m的位置的所有小于a[j]的值都是要计算的,所以可以使用树状数组,以a[j]作为下标,f[i][j]为对应的值,这样每一层来说统计上一层中小于a[j]的个数,就是

bzoj3594: [Scoi2014]方伯伯的玉米田--树状数组优化DP

题目大意:对于一个序列,可以k次选任意一个区间权值+1,求最长不下降子序列最长能为多少 其实我根本没想到可以用DP做 f[i][j]表示前i棵,操作j次,最长子序列长度 p[x][y]表示操作x次后,最高玉米为y时的最长子序列长度 那么以n棵玉米分阶段,对于每个阶段 f[i][j]=max{p[k][l]}+1,  其中k=1 to j , l=1 to a[i]+j 然后用树状数组维护p[][]的最大值 1 #include<stdio.h> 2 #include<string.h&g

[BZOJ3594] [Scoi2014]方伯伯的玉米田 二维树状数组优化dp

我们发现任何最优解都可以是所有拔高的右端点是n,然后如果我们确定了一段序列前缀的结尾和在此之前用过的拔高我们就可以直接取最大值了然后我们在这上面转移就可以了,然后最优解用二维树状数组维护就行了 #include<cstdio> #include<cstring> #include<algorithm> #define N 10005 #define K 505 #define M 5505 using namespace std; inline int read() {

Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp

题目链接:http://codeforces.com/contest/597 A. Divisibility time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Find the number of k-divisible numbers on the segment [a, b]. In other words you need

BZOJ3594 [Scoi2014]方伯伯的玉米田 【树状数组优化dp】

题目链接 BZOJ3594 题解 dp难题总是想不出来,, 首先要观察到一个很重要的性质,就是每次拔高一定是拔一段后缀 因为如果单独只拔前段的话,后面与前面的高度差距大了,不优反劣 然后很显然可以设出\(f[i][j]\)表示前\(i\)个玉米,第\(i\)棵必须选,且共拔高了\(j\)次的最大值 由之前的性质,我们知道\(f[i][j]\)状态中\(i\)的高度是\(h[i] + j\) 所以可以的到状态转移方程: \[f[i][j] = max\{f[k][l]\} + 1 \quad [k