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

来源:互联网 发布:iphonex监控预约软件 编辑:程序博客网 时间:2024/04/30 03:58

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 212354
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|1using 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 0
原创粉丝点击