hdu4991 树状数组辅助DP

Ordered Subsequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 221    Accepted Submission(s): 112


Problem Description
A numeric sequence of a i is ordered if a 1<a 2<……<a N. Let the subsequence of the given numeric sequence (a 1, a 2,……, a N) be any sequence (a i1, a i2,……, a iK), where 1<=i 1<i 2 <……<i K<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others. 

Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
 

Input
Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
 

Output
For each case, output answer % 123456789.
 

Sample Input
  
  
3 2 1 1 2 7 3 1 7 3 5 9 4 8
 

Sample Output
  
  
2 12

题意:从无序串中求长度为k的严格上升序列的个数。

设dp[ i ][ j ]为以位置i的数为结尾的长度为j的严格上升序列的个数。

则dp[ i ][ j ] = sum { dp[ k ][ j - 1 ] | 1 <= k <= i - 1 }


#include <iostream>

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 123456789


const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[M][12];
int pa[M*10],rankk[M];
int head[M*6],vis[M*10];
double dis[M][10];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
vector<int> g[M*10];
ll dp[10005][205];
int has[105][105];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
ll qk_mul(ll a,ll b,ll mo)
{
    ll t=0;
    while(b)
    {
        if(b&1)
            t=(t+a)%mo;
        a=(a<<1)%mo;
        b>>=1;
    }
    t%=mo;
    return t;
}
ll qk_mod(ll a,ll b,ll mo)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=qk_mul(ans,a,mo);
        a=qk_mul(a,a,mo);
        b>>=1;
    }
    ans%=mo;
    return ans;
}
int gcd(int a,int b)
{
    return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a,int b)
{
    return a*b/gcd(a,b);
}
int findx(int t)
{
    if(t!=pa[t])
        pa[t]=findx(pa[t]);
    return pa[t];
}
void unionx(int x,int y)
{
    x=findx(x);
    y=findx(y);
    if(x!=y)
    {
        pa[y]=x;
    }
}


//queue<nodei *>qu;


void init()
{
    for(int i=0; i<101; i++)
        pa[i]=i;
}
int heap[100005];
void push(int x)
{
    int i=++sz;
    while(i>1)  //i>0
    {
        int p=i/2; //(i-1)/2
        if(heap[p]<=x)break;
        heap[i]=heap[p];
        i=p;
    }
    heap[i]=x;
    /* a[++sz] = x;
     int t = a[sz];
     int tson = sz;
     while( (tson > 1)&&( a[tson/2] > t))
     {
         a[tson] = a[tson/2];
         tson = tson/2;
     }
     a[tson] = x;
    */
}
int pop()
{
    int ret=heap[1];//int ret=a[0]
    int x=heap[sz--];//a[--sz]
    int i=1;// i=0
    while(2*i<sz) //2*i+1
    {
        int a=i*2,b=i*2+1;
        if(b<sz&&heap[b]<heap[a])a=b;
        if(heap[a]>=x)break;
        heap[i]=heap[a];
        i=a;
    }
    heap[i]=x;
    return ret;
}
ll a[10010],b[10010];
int lowbit(int i)
{
    return i&(-i);
}
/*
void add(int i,int j,ll v)//修改值并向父节点修改
{
    while(i<=cnt)
    {
        dp[i][j]+=v;//c[i]开始都是0,每经过一个数,它的c[i]+1表示c[i]
        i+=lowbit(i);
    }
}*/
void add(int i,int j,ll v)//修改值并向父节点修改
{
    while(i<=cnt)
    {
        dp[i][j]+=v;//c[i]开始都是0,每经过一个数,它的c[i]+1表示c[i]
        i+=lowbit(i);
    }
}
ll sum(int i,int j)//求和
{
    ll s=0;
    while(i>=1)
    {
        s=(s+dp[i][j])%mod;
        i-=lowbit(i);
    }
    return s;
}
int main()
{
    int i,j,t;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        cnt=(int)(unique(b+1,b+1+n)-(b+1));//去除数组中紧接的重复的值
        memset(dp,0,sizeof(dp));
        //for(i=1;i<=n;i++)dp[i][1]=1;
        for(i=1;i<=n;i++)
        {
            //memset(c,0,sizeof(c));
            int id=lower_bound(b+1,b+1+cnt,a[i])-b; //  求编号(相当于离散化,因为数据原本很大,这样可以缩小到1-10000)   
            add(id,1,1); //树状数组区间初始化 dp[i][1]=1
            for(j=2;j<=m;j++)
            {
                ll s=sum(id-1,j-1);//dp[i][j]=sum(dp[k][j-1])&&k<i&&a[k]<a[i]
                add(id,j,s);// 把 dp 数组的 id位置更新成 s 以便于 下一个 数字的时候 求和
            }

        }
        //for(i=1;i<=n;i++)
            printf("%I64d\n",sum(cnt,m));
        /*ll ans=0;
        for(i=1;i<=n;i++)
            ans=(ans+dp[i][m-1])%mod;
        printf("%I64d\n",ans%mod);*/
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值