CodeForces - 597C:Subsequences (主席树+DP)
2024-09-30 02:25:16
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
Input
5 2
1
2
3
5
4
Output
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 ;
}
最新文章
- Ubuntu 14.04 更换阿里云源
- python asyncio笔记
- poj 1985 Cow Marathon
- wave文件(*.wav)格式、PCM数据格式, goldwave 可以播放pcm raw audio
- Timestamp的作用及与字符串的相互转换 .
- java几种字符串反转
- Problem F: Exponentiation
- maven下的sqlserver配置jar包
- 音频PCM编码
- 通过grub-install命令把grub安装到u盘-总结
- ubuntu日常使用指南
- dp Surf
- js之省市区(县)三级联动效果
- 021.Zabbix的邮件告警-01
- empty是判断一个变量是否为“空”,而isset 则是判断一个变量是否已经设置
- python3: 字符串和文本
- WPF 使用OCX控件速度很慢
- maven常用的一些依赖
- Python线程指南(转)
- 【剑指offer】把二叉树打印成多行,C++实现