题面

【题目描述】

给你一个包含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. Apache的安装与下载

    PHP的运行必然少不了服务器的支持,何为服务器?通俗讲就是在一台计算机上,安装个服务器软件,这台计算机便可以称之为服务器,服务器软件和计算机本身的操作系统是两码事,计算机自身的操作系统可以为linux ...

  2. 4152: [AMPPZ2014]The Captain

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1561  Solved: 620[Submi ...

  3. 在Foxmail中添加阿里云企业邮箱账号

    1.安装完成Foxmail之后,新建账号 输入阿里云邮箱地址和密码,点击创建 接受服务器类型你可以选择POP3或者IMAP,在这里我选择的是POP3 点击创建,大功告成. 为什么要写这篇文章呢? 因为 ...

  4. Asp.net页面生命周期详解任我行(3)-服务器处理请求详细过程

    前言 百度了一下才知道,传智的邹老师桃李满天下呀,我也是邹老师的粉丝,最开始学习页面生命周期的时候也是看了邹老师的视频. 本人是参考了以下前辈的作品,本文中也参合了本人心得,绝非有意盗版,旨在传播,最 ...

  5. Selenium中自动输入10位随机数字的方法

    有时候项目中需要输入快递号,对于已输入过的快递单号则不能再次输入,这种问题怎么解决呢,可以看下这个方法 public static final String ALLCHAR = "01234 ...

  6. maven学习(十)——maven生命周期以及插件

    一.生命周期 1.何为生命周期? Maven生命周期就是为了对所有的构建过程进行抽象和统一,包括项目清理,初始化,编译,打包,测试,部署等几乎所有构建步骤 2.Maven三大生命周期 Maven有三套 ...

  7. (Mac)centos 6.5安装 JDK+mysql

    为了把自己的网站放到外网,购买了阿里云的centos 6.5服务器,以下是安装 JDK 一.JDK安装: 方法一: 1.创建目录,命令行:(这里可以不加sudo) sudo mkdir /jdk 2. ...

  8. 省选算法学习-dp优化-四边形不等式

    嗯......四边形不等式的确长得像个四边形[雾] 我们在dp中,经常见到这样一类状态以及转移方程: 设$dp\left[i\right]\left[j\right]$表示闭区间$\left[i,j\ ...

  9. UVA11806 Cheerleaders (容斥)

    题目链接 Solution 可以考虑到总方案即为 \(C_{nm}^k\) . 考虑到要求的是边缘都必须至少有 \(1\) ,所以考虑不合法的. 第一行和最后一行没有的方案即为 \(C_{(n-1)m ...

  10. vmware虚拟机无法ping通主机,也无法联网