Codeforces 597C Subsequences 【树状数组优化DP】

27 篇文章 0 订阅
10 篇文章 1 订阅

C. Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.

Input

First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.

Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.

Output


Print one integer — the answer to the problem.

Sample test(s)
input
5 2
1
2
3
5
4
output
7



题意:给定n个元素的序列,让你求出长度为k的上升子序列个数。


思路:和CCPC那道题很像,直接用个树状数组转移就好了。时间复杂度O(nlog(n)k)。


AC代码:



#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (50000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000003
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Node{
    int val, id;
};
Node num[MAXN];
bool cmp(Node a, Node b)
{
    if(a.val != b.val)
        return a.val < b.val;
    else
        return a.id > b.id;
}
int n, k;
LL dp[12][MAXN];
int lowbit(int x){
    return x & (-x);
}
void update(int x, int id, LL d)
{
    while(x <= n)
    {
        dp[id][x] += d;
        x += lowbit(x);
    }
}
LL sum(int x, int id)
{
    LL s = 0;
    while(x > 0)
    {
        s += dp[id][x];
        x -= lowbit(x);
    }
    return s;
}
int main()
{
    Ri(n); Ri(k);
    for(int i = 1; i <= n; i++)
        Ri(num[i].val), num[i].id = i;
    sort(num+1, num+n+1, cmp);
    CLR(dp, 0);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= min(k+1, i); j++)
        {
            if(j == 1)
                update(num[i].id, j, 1);
            else
                update(num[i].id, j, sum(num[i].id-1, j-1));
        }
    }
    Pl(sum(n, k+1));
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值