【HDU4991】dp 树状数组优化

Ordered Subsequence

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


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
 

Source
 

题目的意思是求长度为 m 的上升序列有多少个;

设DP[I][J]表示 以第i个元素结尾的 长度为j的序列的个数

dp[i][j]=sum(dp[k][j-1])  (i>k &&a[i]>a[k])

其实就是优化注释掉得部分,离散化之后树状数组求和

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
const int mod = 123456789;
int dp[10005][105];
int a[10005];
int b[10005];
std::map<int, int> mp;
int t[100000];
int n, m;
int Sum(int x)
{
    int sum = 0;
    while (x > 0)
    {
        sum = (sum + t[x]) % mod;
        x -= (x & (-x));
    }
    return sum;
}
void Add(int x, int d)
{
    while (x <= n)
    {
        t[x] = (t[x] + d) % mod;
        x += (x & (-x));
    }
    return;
}
int main()
{
#ifdef DeBUGs
    freopen("1.in", "r", stdin);
#endif

    while (scanf("%d%d", &n, &m) + 1)
    {
        mp.clear();
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
        sort(b + 1, b + n + 1);
        int len = unique(b + 1, b + n + 1) - b - 1;
        for (int i = 1; i <= n; i++)
        {
            mp[b[i]] = i;
        }
        for (int i = 1; i <= n; i++)
        {
            a[i] = mp[a[i]];
            // printf("%d ", a[i]);
        }

        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n; i++)
            dp[i][1] = 1;
        Add(a[1], 1);
        for (int j = 2; j <= m; j++)
        {
            memset(t, 0, sizeof(t));
            for (int i = 1; i <= n; i++)
            {
                // for (int k = 1; k < i; k++)
                //     if (a[i] > a[k])
                //     {
                //         dp[i][j] += dp[k][j - 1];
                //     }
                dp[i][j] = Sum(a[i] - 1);
                Add(a[i], dp[i][j - 1]);
            }
        }

        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans = (ans + dp[i][m]) % mod;
        }
        printf("%d\n", ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值