题面

【题目描述】

给你一个包含n个不同元素的序列,让你求出在这个序列中有多少个长度为k+1的上升子序列。保证答案不会超过8*10^18。

【输入描述】

第一行包括两个正整数n和k(1<=n<=10^5,0<=k<=10)

接下来n行每行包括一个正整数ai(1<=ai<=n),所有ai都是不同的。

【输出描述】

输出一个整数,表示这个问题的答案。

【样例输入】

5 2
1
2
3
5
4

【样例输出】

7

题解

树状数组优化\(O(kn \log n)\)求不下降子序列数.

算是补了ISN那一道题的坑吧.


#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm> namespace Zeonfai
{
inline long long getInt()
{
long long a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1;
while(isdigit(c))
a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const long long N = (long long)1e5;
struct binaryIndexTree
{
long long a[N + 1];
inline void clear()
{
memset(a, 0, sizeof(a));
}
inline void modify(long long u, long long dta, long long bnd)
{
for(; u <= bnd; u += u & - u)
a[u] += dta;
}
inline long long query(long long u)
{
long long res = 0;
for(; u; u -= u & -u)
res += a[u];
return res;
}
}BIT;
int main()
{ #ifndef ONLINE_JUDGE
freopen("CF597C.in", "r", stdin);
#endif using namespace Zeonfai;
long long n = getInt(), k = getInt();
static long long a[N];
for(long long i = 0; i < n; ++ i)
a[i] = getInt();
static long long f[N];
for(long long i = 0; i < n; ++ i)
f[i] = 1;
for(long long i = 0; i < k; ++ i)
{
BIT.clear();
for(long long j = 0; j < n; ++ j)
BIT.modify(a[j], f[j], n), f[j] = BIT.query(a[j] - 1);
}
long long ans = 0;
for(long long i = 0; i < n; ++ i)
ans += f[i];
printf("%lld", ans);
}

Codeforces 597C 子序列的更多相关文章

  1. codeforces 597C (树状数组+DP)

    题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...

  2. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  3. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. CodeForces - 597C Subsequences (树状数组+动态规划)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  6. codeforces 597C - Subsequences

    枚举子序列的末尾,递推. 方案数:f[i = 以i结尾][k =子序列长度] = sum(f[j][k-1]),j < i. 转移就建立k个BIT. #include<bits/stdc+ ...

  7. CodeForces - 597C:Subsequences (主席树+DP)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  8. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...

  9. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

随机推荐

  1. Java基础知识回顾(一):字符串小结

    Java的基础知识回顾之字符串 一.引言 很多人喜欢在前面加入赘述,事实上去技术网站找相关的内容的一般都应当已经对相应知识有一定了解,因此我不再过多赘述字符串到底是什么东西,在官网中已经写得很明确了, ...

  2. SetUnhandledExceptionFilter

    SetUnhandledExceptionFilter 设置未捕获异常处理 通常windows程序长时间运行,会发生各种问题,例如访问异常,内存溢出,堆栈破坏等. 这时候通常希望程序自己能增加处理,而 ...

  3. WCF,WebServices,WebApi区别

    http://www.cnblogs.com/hetring/p/4493137.html

  4. gcc学习记录

    -Wall: 使输出中包含警告信息,提示一些可以避免的错误.如果没有错误,则不会输出信息. -o:后面加上可执行文件的名字.如果不加-o选项,会默认生成a.out可执行文件.举例:gcc -Wall ...

  5. imx6移植librtmp

    一.openssl交叉编译 1.下载 https://www.openssl.org/source/ 版本不要太高,刚开始版本高了,有些函数取消了,链接不上 使用1.0.1f即可 2.编译成共享库 . ...

  6. koa2在node6中如何运行

    koa2在node6下运行 { "babel-core": "^6.24.1", "babel-plugin-syntax-async-functio ...

  7. iOS---Objective-C: +load vs +initialize

    在 NSObject 类中有两个非常特殊的类方法 +load 和 +initialize ,用于类的初始化.这两个看似非常简单的类方法在许多方面会让人感到困惑,比如: 子类.父类.分类中的相应方法什么 ...

  8. ECNU 3263 丽娃河的狼人传说(差分约束)

    丽娃河的狼人传说 Time limit per test: 1.0 seconds Memory limit: 256 megabytes 丽娃河是华师大著名的风景线.但由于学校财政紧缺,丽娃河边的路 ...

  9. [luogu3768] 简单的数学题 [杜教筛]

    题面: 传送门 实际上就是求: 思路: 看到gcd就先反演一下,过程大概是这样: 明显的一步反演 这里设,S(x)等于1到x的和 然后把枚举d再枚举T变成先枚举T再枚举其约数d,变形: 后面其中两项展 ...

  10. flink原理介绍-数据流编程模型v1.4

    数据流编程模型 抽象级别 程序和数据流 并行数据流 窗口 时间 有状态操作 检查点(checkpoint)容错 批量流处理 下一步 抽象级别 flink针对 流式/批处理 应用提供了不同的抽象级别. ...