zoukankan      html  css  js  c++  java
  • The Battle of Chibi(数据结构优化dp,树状数组)

    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 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 i-th number (a_i (1 ≤ a_i ≤ 10^9 )) indicates the value in Cao Cao’s opinion of the i-th 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 ((10^9 + 7)). Note: In the first case, 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 case, Gai Huang has no choice as selecting any 2 information is not in increasing order.

    Sample Input

    2
    3 2
    1 2 3
    3 2
    3 2 1

    Sample Output

    Case #1: 3
    Case #2: 0

    题意

    给定一个长度为(N)的数列A,求A有多少个长度为M的严格递增子序列。输出对(10^9+7)取模后的结果。
    暴力程序还是比较好写的,(dp[i][j])表示到第(i)个数,长度为(j)的序列的个数。
    转移也很好写:
    (dp[i][j]=sum dp[k][j-1]) ((k<i) && (a[k]<a[i]))
    时间复杂度:(O(N^2M))

    #include<bits/stdc++.h>
    using namespace std;
    int read()
    {
        int x=0,w=1;char ch=getchar();
        while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
        return x*w;
    }
    const int N=1010;
    int n,m,mod=1e9+7,ans;
    int a[N],dp[N][N];
    int main()
    {
        int t=read();
        for(int w=1;w<=t;w++)
        {
            memset(dp,0,sizeof(dp));ans=0;
            n=read();m=read();
            for(int i=1;i<=n;i++) a[i]=read();
            dp[0][0]=1;a[0]=-1;
            for(int k=1;k<=m;k++)
            {
    	        for(int i=1;i<=n;i++)
    	        {
    	            for(int j=1;j<i;j++)
    	            {
                        if(a[i]>a[j])
                        dp[i][k]=(dp[i][k]+dp[j][k-1])%mod;
                    }
                }
            }
            for(int i=m;i<=n;i++) ans=(ans+dp[i][m])%mod;
            printf("Case #%d: %d
    ",w,ans);
        }
    }
    

    考虑如何对暴力算法进行优化。
    由于状态的定义,前两重循环无法优化,所以我们对决策的选择上进行优化,将(k)视为定值,每当(i)增大(1),决策候选集合会增加一个元素(dp[i][k-1]),它可能对([i+1,n])的决策作出贡献。我们需要维护一个决策集合,这个决策集合以(a[i])为关键码,(dp[i][k-1])为权值,那么这道题就转化为了一个支持插入和查询前缀和的问题。
    于是我们可以对(a)数组进行离散化,通过树状数组维护前缀和。
    时间复杂度:(O(NMlogN))
    注意:
    1.树状数组不能处理0位置,需要整体往后移一位,在(1)(n+1)上建树状数组。
    2.关于线段树,它被卡常辣qwq

    #include<bits/stdc++.h>
    #define ll(x) (x<<1)
    #define rr(x) (x<<1|1)
    using namespace std;
    int read()
    {
        int x=0,w=1;char ch=getchar();
        while(ch>'9'||ch<'0') {if(ch=='-')w=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
        return x*w;
    }
    const int N=1010;
    int n,m,mod=1e9+7,ans;
    int dp[N][N],c[N],pos[N];
    struct node{
        int v,id;
    }a[N];
    bool cmp(node p,node q){return p.v<q.v;}
    int lowbit(int x){return x&(-x);}
    void add(int p,int v)
    {
        for(int i=p;i<=n;i+=lowbit(i))
            c[i]=(c[i]+v)%mod;
    }
    int query(int x)
    {
        int sum=0;
        for(int i=x;i>0;i-=lowbit(i))
            sum=(sum+c[i])%mod;
        return sum;
    }
    int main()
    {
        int t=read();
        for(int w=1;w<=t;w++)
        {
            memset(dp,0,sizeof(dp));ans=0;
            n=read();m=read();n++;
            for(int i=2;i<=n;i++) a[i].v=read(),a[i].id=i;
            sort(a+2,a+n+1,cmp);
            for(int i=2;i<=n;i++) pos[a[i].id]=i;pos[1]=1;
            add(1,1);
            for(int k=1;k<=m;k++)
            {
                for(int i=2;i<=n;i++)
                {
                    dp[i][k]+=query(pos[i]);dp[i][k]%=mod;
                    add(pos[i],dp[i][k-1]);
                }
                memset(c,0,sizeof(c));
            }
            for(int i=m+1;i<=n;i++) ans=(ans+dp[i][m])%mod;
            printf("Case #%d: %d
    ",w,ans);
        }
    }
    
  • 相关阅读:
    hdu 4107 Gangster 线段树(成段更新)
    hdu 3037 Saving Beans (lucas定理)
    hdu 3944 DP? (Lucas 定理)
    hdu 5038 Grade 水
    ASP.NET Core 配置 MVC
    ASP.NET Core 静态文件
    ASP.NET Core 异常和错误处理
    ASP.NET Core 中间件
    ASP.NET Core 项目配置 ( Startup )
    ASP.NET Core 基本项目目录结构
  • 原文地址:https://www.cnblogs.com/lsgjcya/p/9582853.html
Copyright © 2011-2022 走看看