CodeForces - 597C:Subsequences (主席树+DP)
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·1018.
Input
First line contain two integer values n and k (1 ≤ n ≤ 105, 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.
Output
Print one integer — the answer to the problem.
Examples
5 2
1
2
3
5
4
7
题意:求多少个LIS,其长度为K+1。
思路:用dp[k][x]表示以x结尾的长度为k的LIS。那么暴力的DP复杂度是O(N^2)。
建立k棵线段树,每棵树N个叶子节点,第i棵树的第j个叶子节点表示以j为结尾的长度为i的LIS个数。 那么Tree[i][j]+=Tree[i-1][1到j-1]。
(直接写线段树也可以,这里练习一下主席树。 当然最好写的还是树状数组!
相比的话,主席树应该还是好写的。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=;
int rt[maxn],cnt; ll ans;
struct node
{
int l,r; ll val;
node(){ l=r=;val=;}
node(int L,int R,ll V):l(L),r(R),val(V){}
}s[maxn*];
ll query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now].val;
ll res=; int Mid=(L+R)>>;
if(l<=Mid) res+=query(s[Now].l,L,Mid,l,r);
if(r>Mid) res+=query(s[Now].r,Mid+,R,l,r);
return res;
}
void update(int &Now,int L,int R,int pos,ll val)
{
if(Now==) Now=++cnt;
s[Now].val+=val;
if(L==R) return ; int Mid=(L+R)>>;
if(pos<=Mid) update(s[Now].l,L,Mid,pos,val);
else update(s[Now].r,Mid+,R,pos,val);
}
int main()
{
int N,K,i,j,x;
scanf("%d%d",&N,&K);
for(i=;i<=N;i++){
scanf("%d",&x); x+=;
update(rt[],,N+,x,);
for(j=;j<=K;j++) {
ll tmp=query(rt[j],,N+,,x-);
update(rt[j+],,N+,x,tmp);
}
}
ans=query(rt[K+],,N+,,N+);
printf("%I64d\n",ans);
return ;
}
CodeForces - 597C:Subsequences (主席树+DP)的更多相关文章
- CodeForces - 597C Subsequences (树状数组+动态规划)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- CodeForces960F:Pathwalks (主席树+DP)
You are given a directed graph with n nodes and m edges, with all edges having a certain weight. The ...
- Codeforces.765F.Souvenirs(主席树)
题目链接 看题解觉得非常眼熟,总感觉做过非常非常类似的题啊,就是想不起来=v=. 似乎是这道...也好像不是. \(Description\) 给定长为\(n\)的序列\(A_i\).\(m\)次询问 ...
- Codeforces 893F(主席树+dfs序)
在子树内和距离不超过k是一个二维限制,容易想到主席树,但主席树显然没法查最小值,因为不满足区间可减.kdtree和二维线段树可以干这事,但肯定会T飞.但事实上我们的问题有一个特殊性:对某个点x,查询其 ...
- Optimal Subsequences(主席树)
题意: 给定一个序列,长度为n(<=2e5),有m(<=2e5)个询问,每个询问包含k和pos,要从原序列中选出一个长度为k的子序列,要求是这个序列的和是所有长度为k的序列中最大的,且是字 ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
随机推荐
- GDB调试多线程程序
gdb有thread相关命令,如info thread(简写成info th)显示线程消息,b xx thread yy可以针对某个thread设置断点,thread xx(简写成thr xx)切换到 ...
- 转:Android IOS WebRTC 音视频开发总结 (系列文章集合)
随笔分类 - webrtc Android IOS WebRTC 音视频开发总结(七八)-- 为什么WebRTC端到端监控很关键? 摘要: 本文主要介绍WebRTC端到端监控(我们翻译和整理的,译 ...
- odoo多币种
配置 启用多币种特性,并设置本位币 Gain exchange rate account 汇损收益科目,一般为财务费用下的二级科目 Loss exchange rate account 汇损损 ...
- C# Main(string[] args)方法参数
Main 方法是 C# 控制台应用程序或窗口应用程序的入口点,以下是各种情况下方法参数的输入方式: 1.通过vs启动,需要在“项目属性-调试-启动选项-命令行参数”中配置参数,格式为:参数一 参数二 ...
- LINUX创建用户的命令
LINUX创建用户的命令useradd -g test -d /home/test1 -s /etc/bash -m test1注解:-g 所属组 -d 家目录 -s 所用的SHELL 删除用户命令u ...
- shell grep正则匹配汉字
Shell grep正则匹配中文 测试文本 demo_exe.c,内容如下,需要注意保存的编码格式,对输出到终端有影响: 我们中文操作系统ASNI默认是GBK的. #include<stdio. ...
- angular 复选框checkBox多选的应用
应用场景是这样的,后台返回的数据在页面上复选框的形式repeat出来 可能会有两种需求: 第一:后台返回的只有项,而没有默认选中状态(全是待选状态) 这种情况相对简单只要repeat出相应选项 第二: ...
- Centos7-安装Apache2.4+PHP5.6
linux系统CentOS7先下载Apache需要依赖的软件1.APR下载地址http://apr.apache.org/download.cgiwget下载路径http://mirror.bit.e ...
- Greenplum使用简明手册
GP服务启停 su - gpadmin gpstart #正常启动 gpstop #正常关闭 gpstop -M fast #快速关闭 gpstop –r #重启 gpstop –u #重新加载配置文 ...
- Memcached服务端自动启动(转载)
Memcached服务端自动启动 原文链接:http://www.cnblogs.com/technet/archive/2011/09/11/2173485.html 经测试,要使得Memcach ...