The Battle of Chibi

The Battle of Chibi

Given period of the sequence length n \ (\ {a_i \} \) , where the length is the number of strict requirements rising sequence of m \ (MOD \ ^. 9 + 10. 7 \) , \ (n \ ^ 10. 3 Leq \) .

solution

Difficult to think set \ (F [i] [j] \) represents the position at the end of the i-th, j is the length of LSIS, we have

\[f[i][j]=\sum_{k=1,a_i>a_k}^{i-1}f[k][j-1]\]

Border: \ (F [I] [. 1] =. 1, I = 1,2, ..., n-\) , the remainder being 0

Answer: \ (\ sum_ = {I}. 1 ^ NF [I] [m] \)

Note that this is \ (O (n ^ 3) \) algorithm, priority transfer optimization problem actually is to find the front \ (a_k \) is less than \ (a_i \) of a prefix and f, consider giving a sort, also It is to give a discrete, data structures and the establishment of a prefix (e.g., tree line, Fenwick tree) in which an array of discrete, each query and modify the corresponding prefix position, may then be optimized to \ (O (n ^ 2log ^ n) \)

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define il inline
#define ri register
#define yyb 1000000007
using namespace std;
struct Map{
    int a[1001],b[1001],n;
    il void prepare(int m,int ar[]){
        n=m;
        for(ri int i(1);i<=n;++i)
            a[i]=ar[i];sort(a+1,a+n+1);
        for(ri int i(1);i<=n;++i)b[i]=dfs(ar[i]);
    }
    il int look(int x){
        return b[x];
    }
    il int dfs(int x){
        int l(1),mid,r(n);
        while(l<=r){
            mid=l+r>>1;
            if(x>a[mid])l=mid+1;
            else r=mid-1;
        }return l;
    }
}L;
struct lowbit{
    int n,a[1001];
    il void prepare(int m){
        n=m,memset(a,0,sizeof(a));
    }
    il void change(int p,int v){
        while(p<=n)(a[p]+=v)%=yyb,p+=-p&p;
    }
    il int ask(int p){
        int ans(0);
        while(p)(ans+=a[p])%=yyb,p-=-p&p;return ans;
    }
}ar;
int a[1001],dp[1001][1001];
il void read(int&),work();
int main(){
    int lsy,i;read(lsy);
    for(i=1;i<=lsy;++i)printf("Case #%d: ",i),work();
    return 0;
}
il void work(){
    int n,m;read(n),read(m);
    for(int i(1);i<=n;++i)read(a[i]);
    L.prepare(n,a),memset(dp,0,sizeof(dp));
    for(int i(1);i<=n;++i)dp[i][1]=1;
    for(int i,j(2);j<=m;++j){
        ar.prepare(n);
        for(i=1;i<=n;++i)
            dp[i][j]=ar.ask(L.look(i)-1),
                ar.change(L.look(i),dp[i][j-1]);
    }int ans(0);
    for(int i(1);i<=n;++i)(ans+=dp[i][m])%=yyb;
    printf("%d\n",ans);
}
il void read(int &x){
    x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/10958341.html