题面

【题目描述】

给你一个包含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. Docker背后的内核知识(一)

    Docker背后的内核知识 当谈论Docker时,常常会聊到Docker的实现方式.很多开发者都知道,Docker容器本质上是宿主机上的进程.Docker通过namespace实现了资源隔离.通过cg ...

  2. windows下,cmd 运行 python 脚本,选中文字就停止运行了【已解决】

    参考资料: https://jingyan.baidu.com/article/ce09321bb95dda2bff858f26.html 问题原因: cmd 里面,快速编辑模式会暂停程序 解决步骤: ...

  3. Quorum机制与NRW算法总结

    Quorum机制与NRW算法总结 1.Quorum机制 Quorum,原指为了处理事务.拥有做出决定的权力而必须出席的众议员或参议员的数量(一般指半数以上). 2.NRW算法 NRW算法是基于Quor ...

  4. spring整合mybatis详解

    在上篇螃蟹已经说明spring注解的最经典配置,接下来开始整合mybatis,这样整个项目就相对完整了. 有关本实例的源码可以到 <spring MVC注解实例及说明文档> 下载. 如需转 ...

  5. 安恒杯月赛 babypass getshell不用英文字母和数字

    BABYBYPASS 先贴代码: ①限制字符长度35个 ②不能使用英文字母和数字和 _ $ 最后提示有个getFlag()函数,从这个函数入手. 我们的第一思路是直接eval执行getFlag函数,但 ...

  6. PostgreSQL 配置内存参数

    对于任何数据库软件,内存配置项都是很重要的配置项.在 PostgreSQL 主要有以下几个内存配置参数. shared_buffers: integer 类型,设置数据库服务器将使用的共享内存缓冲区数 ...

  7. Leetcode 553.最优除法

    最优除法 给定一组正整数,相邻的整数之间将会进行浮点除法操作.例如, [2,3,4] -> 2 / 3 / 4 . 但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级.你需要找出怎么添 ...

  8. Unity 脚本<2>

    UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { [HideInInspector ...

  9. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

  10. redhat linux 7.4关闭透明大页

    每一步: 在GRUB_CMDLINE_LINUX加入选项 transparent_hugepage=never echo 'GRUB_CMDLINE_LINUX="transparent_h ...