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 ...
随机推荐
- 【原】objc_setAssociatedObject和objc_getAssociatedObject
本文转载请注明出处--polobymulberry-博客园 两个函数名称中都有associate,意思是关联,这里的关联表示的是一种 从属关系,即有一个关联者和被关联者,我们说NSArray的对象ar ...
- 哈夫曼(huffman)树和哈夫曼编码
哈夫曼树 哈夫曼树也叫最优二叉树(哈夫曼树) 问题:什么是哈夫曼树? 例:将学生的百分制成绩转换为五分制成绩:≥90 分: A,80-89分: B,70-79分: C,60-69分: D,<60 ...
- (转)ubuntu apache2 的负载均衡和反向代理
apache功能其实很是强大,最近一段时间研究了下apache的反向代理和负载均衡,反向代理网协的外网出口和我的博客出口就是通过apache的反向代理实现的,总结一下,重点说说负载均衡的配置. 首先说 ...
- spring aop
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...
- Oracle RAC 更换存储实验
实验环境准备: RHEL 6.5 + Oracle 11.2.0.4 RAC (2nodes) OCR和Voting Disk使用的是OCR1磁盘组,底层对应3个1G大小的共享LUN,一般冗余: DA ...
- GDB调试命令
1.查看源码: list [函数名][行数] 2.暂停程序 (1)设置断点: a.break + [源代码行号][源代码函数名][内存地址] b.break ... if condition .. ...
- .NET DateTime类型变量作为参数时设置默认值
一个小的 Tips. .NET 中函数参数的默认值需要是编译时常量.如果参数是引用类型,可以设置Null,如果是值类型,可以设置相应的编译时常量,如整型可以用整数,但对于DateTime(结构体,值类 ...
- php:ci学习笔记1
ci下载的开发包: phpstudy的部署: phpstudy的根目录是:D:\WWW 新建目录 cms 把ci开发包的application system index.php lic ...
- 【挖财工作笔记】idea使用指南
一 安装破解 破解选择服务器,然后选择地址:http://www.iteblog.com/idea/key.php http://idea.iteblog.com/key.php http://i ...
- 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />
在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...