Testing Round #12 C
Description
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
题意:求长度为k+1的上升子序列有多少个
解法:sum=dp[0][k+1]+dp[1][k+1]+dp[2][k+1]+....dp[n][k+1]
dp[x][y]是x为上升子序列最后一个元素,长度为y的个数
用树状数组维护,更新的是num为上升子序列最后一个元素,长度为j时,加上num为上升子序列最后一个元素,长度为j-1时个数
最后求sum(n,m+1)总和
1 #include <bits/stdc++.h> .h> 2 using namespace std; 3 #define ll long long 4 ll n,m; 5 ll dp[200000][20]; 6 ll bit(ll x) 7 { 8 return x&(-x); 9 } 10 void up(ll x,ll y,ll ans) 11 { 12 while(x<200000) 13 { 14 dp[x][y]+=ans; 15 x+=bit(x); 16 } 17 } 18 ll sum(ll x,ll y) 19 { 20 ll ans=0; 21 while(x>0) 22 { 23 ans+=dp[x][y]; 24 x-=bit(x); 25 } 26 return ans; 27 } 28 int main() 29 { 30 cin>>n>>m; 31 up(1,0,1); 32 for(int i=1;i<=n;i++) 33 { 34 ll num; 35 cin>>num; 36 for(int j=m+1;j>=1;j--) 37 { 38 up(num,j,sum(num,j-1)); 39 } 40 } 41 cout<<sum(n,m+1)<<endl; 42 return 0; 43 }
分类:
codeforces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~