597C. Subsequences【DP】【树状数组】
来源:互联网 发布:淘宝发布助手一直提示 编辑:程序博客网 时间:2024/05/16 11:59
题目链接
http://codeforces.com/problemset/problem/597/C
思路
就是叫你求长度为k+1(直接当k吧,读完加个1就行)的上升子序列个数。
设dp[i][j]为以a[i]结尾的长度为j的上升子序列个数,则可以写出方程:
dp[i][j]=
那么最终答案就是ans=
然而直接写是
然而这时候就碰到一个问题了,a[k] < a[i]这个条件怎么表达,树状数组不带条件判断功能呀(一脸懵比)。
网上搜了下大神们的代码,发现了一个神奇的技巧。
就是数组中元素不按它原来的顺序存,而是以a[i]为下标存,这样只要i是增序历遍的,任意时刻数组中已经存在的数就是所有的a[1]~a[i]了,这时候就可以直接求和1~a[k]-1了。
AC代码
#include <iostream>#include <cstdio>using namespace std;typedef long long ll;int a[100000+100];ll dp[100000+100][20];ll biArray[20][100000+100];int n,k;void add(int index, int k, ll num){ while(k<=n) { biArray[index][k]+=num; k+=k&-k; }}ll read(int index, int k){ ll sum=0; while(k) { sum+=biArray[index][k]; k-=k&-k; } return sum;}int main(){ scanf("%d%d",&n,&k); k++; for(int i=1 ; i<=n ; ++i) { scanf("%d",&a[i]); } ll ans=0; for(int i=1 ; i<=n ; ++i) { dp[i][1]=1; add(1,a[i],1); for(int j=2 ; j<=k && j<=i ; ++j) { dp[i][j]=read(j-1,a[i]-1); add(j,a[i],dp[i][j]); } ans+=dp[i][k]; } printf("%I64d\n",ans); return 0;}
0 0
- Codeforces 597C Subsequences dp + 树状数组
- 597C. Subsequences【DP】【树状数组】
- Subsequences CodeForces - 597C 树状数组 + dp
- Codeforces 597C Subsequences 【树状数组优化DP】
- Codeforces 597C Subsequences【Dp+二维树状数组】
- codeforces-597C-Subsequences【树状数组】
- CF - 314C - Sereja and Subsequences(树状数组+dp)
- [dp+树状数组优化] CF597C. Subsequences
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- Codeforces Testing Round #12C - Subsequences (dp+树状数组优化)
- Lightoj 1085 All Possible Increasing Subsequences (树状数组+DP)
- hdu 2227 Find the nondecreasing subsequences 树状数组+简单DP
- hdu2227---Find the nondecreasing subsequences(dp+树状数组)
- hdoj 2227 Find the nondecreasing subsequences 【树状数组优化dp】
- lightoj 1085 - All Possible Increasing Subsequences 【树状数组优化dp】
- hdu 2227 Find the nondecreasing subsequences(树状数组+DP)
- Codeforces 597C(树状数组 + dp)
- codeforces 597C (树状数组+DP)
- iOS中使用RegexKitLite来试用正则表达式 使用ARC 20个错误解决办法
- 245,键盘处理
- SHUOJ 1013 过河卒问题 (递推)
- The Java™ Tutorials — Concurrency :Defining and Starting a Thread 线程的定义和启动
- 原子位操作
- 597C. Subsequences【DP】【树状数组】
- Android TV Study 1-1
- 在自定义drawRect setBackground不起作用的原因
- 阅读整理《从0到1》
- linux下JDK的安装详解及第一个java应用程序
- [Spring实战系列](15)使用Spring基于Java的配置
- System.out.println原理
- hdu 1061 快速幂取模
- Android开发笔记(六十一)文件下载管理DownloadManager