HDU - 5542 The Battle of Chibi(LIS+树状数组优化)

The Battle of Chibi

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 NN information to be leaked, in happening order. Each of the information was estimated to has aiai 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 MM information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the NN information and just select MM of them. Find out how many ways Gai Huang could do this.

InputThe first line of the input gives the number of test cases, T(1≤100)T(1≤100). TT test cases follow.

Each test case begins with two numbers N(1≤N≤103)N(1≤N≤103) and M(1≤M≤N)M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then NN numbers in a line, the ithith number ai(1≤ai≤109)ai(1≤ai≤109) indicates the value in Cao Cao‘s opinion of the ithith information in happening order.OutputFor each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy 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)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.

题意:求一个序列中长度为m的最长上升子序列个数。

设dp[i][j]表示以当前位置i结束的长度为j的序列个数。

容易想到O(n^3)的做法。开始想用记忆化搜索将复杂度降低一些,再加上若干剪枝,仍然会T。

考虑到问题涉及小于(大于)某个数的数的和,因此应该想到用树状数组优化。

使用lower_bound将所有数离散化,还要注意在dp的同时更新树状数组,时间复杂度为O(n^2*logn)。

//注释部分为优化前

#include<bits/stdc++.h>
#define MAX 1005
#define MOD 1000000007
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;

int a[MAX],b[MAX];
int n,m;
ll dp[MAX][MAX];

ll sum(int x,int y){
    ll ans=0;
    while(1<=x){
        ans+=dp[x][y];
        ans%=MOD;
        x-=lowbit(x);
    }
    return ans;
}
void add(int x,int y,int z){
    while(x<=n){
        dp[x][y]+=z;
        dp[x][y]%=MOD;
        x+=lowbit(x);
    }
}
int main()
{
    int tt=0,t,i,j,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b+1,b+n+1);
        for(i=1;i<=n;i++){
            a[i]=lower_bound(b+1,b+n+1,a[i])-b;
        }
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++){
            //dp[i][1]=1;
            add(a[i],1,1);
            for(j=2;j<=m;j++){
                //dp[i][j]=dp[i-1][j];
                //for(k=1;k<i;k++){
                //    if(a[k]<a[i]){
                //        dp[i][j]+=dp[k][j-1];
                //        dp[i][j]%=MOD;
                //    }
                //}
                ll temp=sum(a[i]-1,j-1);
                add(a[i],j,temp);
            }
        }
        //printf("Case #%d: %I64d\n",++tt,dp[n][m]);
        printf("Case #%d: %I64d\n",++tt,sum(n,m));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yzm10/p/9774771.html

时间: 2024-10-03 07:10:51

HDU - 5542 The Battle of Chibi(LIS+树状数组优化)的相关文章

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao 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.

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

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)的,显然

LIS 树状数组优化

lis 众所周知 即最长上升子序列 可以用dp求解 复杂度O(n^2) 我们考虑优化 用树状数组(或者线段树) 树状数组维护区间最大值 (省去原始O(n^2)算法中的查找) 这样还能求出以i结尾的lis 二分只能求出当前序列的lis (许多题里要求lis个数什么的qwq 总之比二分方便 除了码量长) 还有一个小点就是可能会用到离散化(传送门) 具体实现看代码 #include<bits/stdc++.h> using namespace std; int a[mxn],b[mxn],n,sz,

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences                                  Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                             

hdu 3584 二进制0,1反转 三维树状数组 及三维树状数组模板

先贴自己类比二维树状数组写的三维树状数组模板: 开始的时候循环体内j=y,k=z,没写,以为自己思路错了,,,hehe..... 更高维的树状数组以此类比 const int MAXN = 100+10; int c[MAXN][MAXN][MAXN];int X,Y,Z; int N; inline int lowbit(int x){return x&(-x);} void update(int x, int y, int z, int v) { int i=x,j=y,k=z; while

HDU 4417 类似求逆序数的树状数组

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2250    Accepted Submission(s): 1092 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability

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,按照区间的端点升序排序(左端点第一关键字,右端点第二关键字) 问题转化为能否用剩