关闭

树状数组+dp(不太懂) Codeforces 597C

680人阅读 评论(0) 收藏 举报
分类:
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

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long n;
long long dp[12][100010];
long long query(long long x,long long i)
{
    long long ans=0;
    while(i>0)
    {
        ans+=dp[x][i];
        i-=i&-i;
    }
    return ans;
}
void update(long long x,long long i,long long num)
{
    while(i<=n)
    {
        dp[x][i]+=num;
        i+=i&-i;
    }
    return ;
}
int   main()
{
    long long  k;
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(long long  i=0; i<n; i++)
        {
            long long  x;
            scanf("%lld",&x);
            for(long long j=1; j<=k+1; j++)
            {
                long long res;
                if(j==1)
                    res=1;
                else res=query(j-1,x-1);
                update(j,x,res);
            }
        }
        printf("%lld\n",query(k+1,n));

    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19149次
    • 积分:787
    • 等级:
    • 排名:千里之外
    • 原创:60篇
    • 转载:4篇
    • 译文:1篇
    • 评论:3条
    最新评论