codeforces 597C C. Subsequences(dp+树状数组)
题目链接:
1 second
256 megabytes
standard input
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·10^18.
First line contain two integer values n and k (1 ≤ n ≤ 10^5, 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.
Print one integer — the answer to the problem.
Examples
5 2
1
2
3
5
4
7 题意: 问在有n个不同的数组成的序列中,有k+1个数的递增子序列的个数是多少; 思路:
k不大n也不大看起来好像是dp,假设dp[i][j]表示在前i个数中长度为j的递增子序列并且a[i]是这个序列的最后一位的个数;
ans[j]=dp[1][j]+dp[2][j]+...+dp[n][j];
dp[i][j]=dp[x][j-1](x为按a[]序列中数值比它小且在它前面的)
举一个例子:第一行为输入的a[]第二行为最后一个为递增子序列且最后位为a[i]的个数,再把第二行做成一个树状数组再查询,k-1次后就得到结果
| 6 | 10 | 9 | 7 | 1 | 2 | 8 | 5 | 4 | 3 | ans |
| 0 | 1 | 1 | 1 | 0 | 1 | 4 | 2 | 2 | 2 | 14 |
| 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | 1 | 1 | 5 |
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n,k;
long long dp[N],sum[N],b[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,long long num)
{
while(x<=n)
{
sum[x]+=num;
x+=lowbit(x);
}
}
long long query(int x)
{
long long s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
b[i]=;
scanf("%d",&a[i]);
dp[i]=query(a[i]);
update(a[i],b[i]);
}
for(int i=;i<=k;i++)
{
memset(sum,,sizeof(sum));
for(int j=;j<=n;j++)
{
b[j]=dp[j];
dp[j]=query(a[j]);
update(a[j],b[j]);
}
}
long long ans=;
for(int i=;i<=n;i++)
{
ans+=dp[i];
}
if(!k)cout<<n<<"\n";//注意k==0的情况
else cout<<ans<<"\n";
return ;
}
codeforces 597C C. Subsequences(dp+树状数组)的更多相关文章
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- CodeForces - 314C Sereja and Subsequences (树状数组+dp)
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
随机推荐
- jQuery 插件-(初体验一)
1.jquery有2个扩展方法: jquery.fn.extend=jquery.prototype.extend jquery.extend (两者的区别放在后面文章说) 2.具体实例结构: //创 ...
- Compile FreeCAD on Windows
Compile FreeCAD on Windows eryar@163.com 1.Introduction FreeCAD是一个参数化的三维造型软件,主要用于任意大小的实际模型的设计.参数化的建模 ...
- OpenCASCADE Job - Shoe Doctor
鞋博士 鞋博士经过8年沉淀,在鞋类工业4.0全流程平台上积累了相当的技术实力,获投资商亲睐. 新的一年,在投资商协助下,将踏上新的征途,因此诚邀您加盟顶层技术合伙人. 如果您具备以下实力,我们期待您的 ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档
原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...
- 打造高效前端工作环境-tmuxinator
前言 虽然tmux能让我们方便组织工作环境,但每次重新打开会话时都需要手动重新创建窗口.窗格和执行各种程序,能不能像VS那样以工程为单位保存窗口.窗格和各种所需执行的程序的信息呢?tmuxinato ...
- UWP简单示例(二):快速开始你的3D编程
准备 IDE:Visual Studio 2015 了解并学习:SharpDx官方GitHub 推荐Demo:SharpDX_D3D12HelloWorld 第一节 世界 世界坐标系是一个特殊的坐标系 ...
- helios架构详解(二)客户端架构和cluster
helios的客户端架构和服务器端类似,只有部分类有所区别(黄色的),下图是客户端的架构 可以看出实际上只有IConnection的俩个实例(TcpConnection.UdpConnection)是 ...
- Yii 2.x Behavior - 类图
yii\base\Component 继承这个类的类都具备扩展行为的能力