HDOJ 5542 The Battle of Chibi

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

题目大意:在n个数中找长度为m的单调上升子序列有多少种方案

题目思路:DP,离散化,树状数组优化,

dp[i][j]代表大小为i的数 长度为j时的方案,状态转移方程dp[i][j]=simiga(dp[1..i-1][j-1]) 如果直接求和的话,复杂度是n^3不行

用树状数组优化求和 复杂度n^2logn

n<=1000,a[i]<=1e9,所以离散化搞一下就行

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=1100;
 7 const long long  mod=1e9+7;
 8 long long  tree[maxn][maxn];
 9 long long dp[maxn][maxn];
10 long long  a[maxn],b[maxn];
11 void add(int i,int m,int k){
12     for(i;i<=maxn;i+=(i&(-i))) (tree[i][m]+=k)%=mod;
13 }
14 long long  query(int i,int m){
15     long long sum=0;
16     for(i;i>0;i-=(i&(-i))) (sum+=tree[i][m])%=mod;
17     return sum%mod;
18 }
19 void init(){
20     memset(dp,0,sizeof(dp));
21     memset(tree,0,sizeof(tree));
22 }
23 void solve(int T){
24     printf("Case #%d: ",T);
25     int n,m;
26     scanf("%d %d",&n,&m);
27     init();
28     for(int i=1;i<=n;i++) scanf("%lld",&b[i]),a[i]=b[i];
29     sort(b+1,b+1+n);
30     int size=unique(b+1,b+1+n)-b;
31     for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+size,a[i])-b;
32     for(int i=1;i<=n;i++){
33         for(int j=1;j<=min(i+1,m);j++){
34             if(j==1) dp[j][a[i]]=1;
35             else
36             dp[j][a[i]]=query(a[i]-1,j-1)%mod;
37             add(a[i],j,dp[j][a[i]]);
38         }
39     }
40     printf("%lld\n",query(size+1,m)%mod);
41 }
42 int main(){
43     int T;
44     scanf("%d",&T);
45     for(int i=1;i<=T;i++) solve(i);
46 }
时间: 2024-11-02 23:26:12

HDOJ 5542 The Battle of Chibi的相关文章

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 loya

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结尾的长度为j的严格上升子序列有多少个 dp[i][j]=sum{dp[k][j-1]},k小于i且a[k]<a[i] 区间dp,复杂度为O(n^3) 会超时,第三层循环求和用树状数组加速 时间复杂度为O(n^2logn) 离散化的时候排序后不去重(否则WA) [Accepted] #include<

hdu 5542 The Battle of Chibi(2015CCPC - C题)

题目链接:hdu 5542 首届CCPC的C题,比赛时一起搞了好久,最后是队友A出的,当时有试过用树状数组来优化 dp,然后今天下午也用树状数组搞了一下午,结果还是踩了和当时一样的坑:我总是把用来记录状态的 dp 数组和树状数组里的内置数组混在一起使用了,而且两重循环的顺序也反了,以至于两组数据 3 2               3 2 1 2 3     和     3 2 1 程序跑都得出了相同的结果,无语...之前做 dp 和线段树结合的题时也是傻傻地分不清,说到底就是 dp 的功力很不

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 (ccpc 南阳 C)(DP 树状数组 离散化)

题意:找出长度为n的序列中 递增序列长度为m的个数. 分析:dp[i][j] = sum(dp[1][j-1]+dp[2][j-1]+~+dp[i-1][j-1]) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #include <cstdlib>

2015南阳CCPC C - The Battle of Chibi DP

C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 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

CDOJ 1217 The Battle of Chibi

The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 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

The Battle of Chibi

The Battle of Chibi 给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\). 解 不难想到设\(f[i][j]\)表示以第i个位置结尾,长度为j的LSIS,因此我们有 \[f[i][j]=\sum_{k=1,a_i>a_k}^{i-1}f[k][j-1]\] 边界:\(f[i][1]=1,i=1,2,...,n\),其余为0 答案:\(\sum_{i=1}^nf[i][m]\) 注意到这是\