首页 > 其他分享 >HDU 5542 The Battle of Chibi(树状数组+dp)

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

时间:2023-06-02 18:34:59浏览次数:40  
标签:information HDU Gai 5542 int Cao Huang Chibi dp


The Battle of Chibi


Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1749    Accepted Submission(s): 621




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

a

i

 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


 



Input


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 

i

th

 number 

a

i

(1≤a

i

≤10

9

)

 indicates the value in Cao Cao's opinion of the 

i

th


 



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(10

9

+7)

.


 



Sample Input


2 3 2 1 2 3 3 2 3 2 1


 



Sample Output


Hint


 



Source


The 2015 China Collegiate Programming Contest 


【解析】:

原本用dp退出来了一个状态方程,dp[i][j]表示以第j个元素结尾的长度为i的递增子序列数目(注意,包含第j元素)


如此实现的话:

dp[i][j]  +=  dp[i-1][t],意思是当前状态来自:不包含第j元素,长度为i-1时的,所有符合a[t]<a[j]的,dp[i-1][t]的累加之和。


尝试着写了下,很遗憾需要三层循环维护,时间复杂度太高,注定超时。


for(int i=1;i<=m;i++)//长度   
       {  
        if(i==1)dp[i][j]=1;//长度为1时特殊处理   
        else  
           for(int j=i;j<=n;j++)//遍历元素   
           {  
            for(int t=1;t<j;t++)  
                if(a[t]<a[j])//累加前面的数目   
                    dp[i][j]+=dp[i-1][t];  
        }  
       }



然后带着这种思想,用树状数组把最里面那层循环优化掉,总复杂度变为O(n*n*log n),给了4秒还是很险啊。


我用long long的数据提交时超时的,全改成int,竟然奇迹般的过了,耗时3556ms


这里有一个技巧,由于输入的数据最大10的9次方,所以需要在不改变原数据大小关系的基础上,降低数据大小。

   例如:1  4  7  4  9

可降为:1  2  3  2  4

使得树状数组的下标得以降低,不爆树状数组的下标

下面的代码中,用函数g()处理的,处理后存入数组t

【代码】:


#include <stdio.h>  
#include <stdlib.h>    
#include <string.h>    
#include <iostream>    
#include <algorithm>   
using namespace std;  
const int mod=1e9+7;  
struct node{  
    int id;  
    int a;  
}p[1200];  
int a[1200];  
int dp[1200][1200];  
int t[1200];  
int n,m;  
void add(int i,int k,int num)    
{      
    while(k<=n)    
    {      
        dp[i][k]+=num;  
        dp[i][k]%=mod;     
        k+=k&-k;  
    }      
}     
int read(int i,int k)//长度i时,1~k的区间和      
{      
    int sum=0;      
    while(k)      
    {      
        sum+=dp[i][k];   
        sum%=mod;     
        k-=k&-k;  //这里是逆序的,所以累减    
    }      
    return sum;      
}   
  
bool cmp(node f1,node f2)  
{  
    return f1.a<f2.a;  
}  
int g()  
{//p是结构体   
    for(int i=1;i<=n;i++)  
    {  
        p[i].a=a[i];//原数据   
        p[i].id=i;//记住下标   
    }  
    sort(p+1,p+n+1,cmp);//数组下标从1开始   
    int k=1;  
    for(int i=1;i<=n;i++)  
    {  
        if(i>1&&p[i].a!=p[i-1].a)k++;  
        t[p[i].id]=k;//不变序缩小原数据  
    }  
    return k;//缩距后最大数据  
}  
int main()  
{  
    int T,r=1;  
    scanf("%d",&T);  
    while(T--)  
    {  
        scanf("%d%d",&n,&m);  
        memset(dp,0,sizeof(dp));  
        for(int i=1;i<=n;i++)  
            scanf("%d",a+i);  
        int k=g();//预处理t数组  
        for(int j=1;j<=n;j++)//加入第j个元素  
        {  
            for(int i=1;i<=j;i++)  
            {  
                if(i==1) add(i,t[j],1);  
                else  
                    add(i,t[j],read(i-1,t[j]-1));//把比t[j]小的累加进t[j]  
            }  
        }  
        int ans=read(m,n)%mod;  
        printf("Case #%d: %d\n",r++,ans);  
    }  
}




标签:information,HDU,Gai,5542,int,Cao,Huang,Chibi,dp
From: https://blog.51cto.com/u_16125110/6404539

相关文章

  • ICPC2015(沈阳)HDU5521 建图技巧+最短路
    MeetingTimeLimit:12000/6000MS(Java/Others)  MemoryLimit:262144/262144K(Java/Others)TotalSubmission(s):3533  AcceptedSubmission(s):1136ProblemDescriptionBessieandherfriendElsiedecidetohaveameeting.However,afterFarmerJohndecor......
  • HDU2227(非降子序列的个数)
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2227题意:给定一个长度为n(n<=100000)的整数序列,求其中的非降子序列的个数。分析:如果n的值比较小,那么就是一个纯粹的dp题。设dp[i]表示以a[i]为结尾非降子序列的个数,其状态转移方程为:      可以看出,这样做的时间复杂度是......
  • HDU4372(第一类斯特林数)
    题目:CounttheBuildings题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数。0<N,F,B<=2000首先我们知道一个结论:n的环排列的个数与n-1个元素的排列的个数相等,因为P(n,n)/n=(n-1)!。可以肯定,无论从最左边还是从最右边看,......
  • HDU4633(Polya计数)
    题目:Who'sAuntZhang#include<iostream>#include<string.h>#include<stdio.h>usingnamespacestd;typedeflonglongLL;constLLMOD=10007;LLquick_mod(LLa,LLb){LLans=1;a%=MOD;while(b){if(b&1......
  • HDU4382(特殊的矩阵连乘)
    题目:HarryPotterandCyberSequenceGenerator题意,有两个容器C1,C2,初始的时候C1中有一个数的值为V,给你K个操作,每次都重复这K个操作N遍,最后问你C2中的数是   多少。N<=10^100。1:循环操作的次数巨大,敏感的想到这是矩阵连乘的题目。2:K个操作可以得出一个矩阵,N个K操作就是这个......
  • HDU1524(博弈--有向无环图SG函数)
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1524题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步,如果到某一步玩家不能移动时,那么这个人就输.分析:本题是最典型的有向无环图的博弈,利用dfs把所有顶点的SG值......
  • HDU3662(求三维凸包表面的多边形个数,表面三角形个数,体积,表面积,凸包重心,凸包中点到面
    题目:3DConvexHull题意:给定空间中的n个点,求这n个点形成的凸包的表面的多边形个数。增量法求解:首先任选4个点形成的一个四面体,然后每次新加一个点,分两种情况:1>在凸包内,则可以跳过2>在凸包外,找到从这个点可以"看见"的面S(看不看得见可以用法向量,看点是否在面外侧),删除这些......
  • HDU3113(工科数学分析之分解)
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3113题意:给出一个正整数n,范围是[1,1000000],求出满足方程的一组整数解,要求x最小。分析:这个方程与平方和不同的是,加号两边的任意一个可以为负数,所以直接枚举然后Hash就显得不好做了。那么我用一种比较有效的方式解决。我们知道,那么我......
  • HDU2608 0 or 1
    题目:0or1 分析: 首先看T(n),通过找规律可以发现:(看了别人的解题报告才知道这部分是怎么推出来的) 仅当n为1,2,4,8,9,16,18,25,32,36,49,50,64,72,81,98,100…………的时候,T(n)%2==1; 可以发现,if(n%(i*i)==0||n%(i*i*2)==0)=>T(n)%2==1; 然后对S(n),只要看在n前面,有几个T(k)是为1即可......
  • HDU1016(DFS)
    题目:PrimeRingProblem #include<stdio.h>#include<string.h>#defineN105intn;inta[25];boolvisited[25];boolprime[N];voidisprime(){inti,j;memset(prime,true,sizeof(prime));for(i=2;i<N;i++){for(j=i+......