题意:给一串数字,问长度为m的严格上升子序列有多少个

解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数,

则有dp[i][j] = SUM(dp[k][j-1])  (a[k] < a[i] && k < i)

不可能直接遍历,所以考虑优化,可以看出dp方程相当于一个区间求和,所以可以用树状数组来优化。

代码:

#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod 123456789
#define lll __int64
using namespace std;
#define N 10007 lll c[N],a[N],b[N];
int n,m;
lll dp[N][]; int lowbit(int x)
{
return x & (-x);
} void modify(int pos,lll val)
{
while(pos <= n)
{
c[pos] += val;
pos += lowbit(pos);
}
} lll getsum(int pos)
{
lll res = ;
while(pos > )
{
res = (res+c[pos])%SMod;
pos -= lowbit(pos);
}
return res;
} int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
b[i] = a[i];
}
sort(b+,b+n+);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
dp[i][] = ;
for(j=;j<=m;j++)
{
memset(c,,sizeof(c));
for(i=;i<=n;i++)
{
int ind = lower_bound(b+,b+n+,a[i])-b;
dp[i][j] = getsum(ind-);
modify(ind,dp[i][j-]);
}
}
lll ans = ;
for(i=;i<=n;i++)
ans = (ans + dp[i][m])%SMod;
printf("%I64d\n",ans);
}
return ;
}

HDU 4990 Ordered Subsequence --数据结构优化DP的更多相关文章

  1. POJ2533——Longest Ordered Subsequence(简单的DP)

    Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...

  2. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  3. 数据结构优化dp

    本以为自己的dp已经成熟了没想到在优化上面还是欠佳 或者是思路方面优化dp还不太行. 赤壁之战 当然 很有意思的题目描述 大体上是苦肉计吧 .盖黄 ... 题意是 求出长度为m的严格上升子序列的个数 ...

  4. hdu 3507 Print Article(斜率优化DP)

    题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...

  5. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  6. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  7. HDU 2829 Lawrence(斜率优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  8. HAOI2008 木棍分割 数据结构优化dp+二分答案

    很久之前打的题,现在补篇博客 打滚动数组 #E. 木棍分割 Accepted 100 1712 ms 1512 KiB   2019-05-07 17:01:23 Short 不打滚动数组 #419. ...

  9. HDU 5009 Paint Pearls 双向链表优化DP

    Paint Pearls Problem Description   Lee has a string of n pearls. In the beginning, all the pearls ha ...

随机推荐

  1. ServiceStack.Text反序列化lowercase_underscore_names格式的JSON

    代码: [Test] public void Test() { JsConfig.PropertyConvention = JsonPropertyConvention.Lenient; var js ...

  2. 用SQL语句操作数据

    转载请注明出处:http://www.cnblogs.com/smbk/ 1.点击[新建查询]按钮,打开SQL命令编辑框,对数据库表的操作以及维护都可以通过编辑SQL命令实现. 2.在编辑框内编辑创建 ...

  3. 在IntelliJ IDEA14中安装go语言插件

    go语言的集成开发环境仍不成熟,试用了liteide,感觉很不适应,弹出菜单对程序员的干扰太大.所以就试大牌的IntelliJ IDEA,这工具本来是JAVA开发阵营的,不过它已经变为一个非常强大的支 ...

  4. Android 身份证号码查询、手机号码查询、天气查询

    1.基本信息 身份证号码查询:http://apistore.baidu.com/apiworks/servicedetail/113.html 手机号码:http://apistore.baidu. ...

  5. iOS打包ipa select a method for export几个选项的意思

    他们的意思分别为:Save for iOS App Store Deployment保存到本地 准备上传App Store 或者在越狱的iOS设备上使用,需要提供发布证书
Save for Ad Ho ...

  6. xib命名注意事项--防止被其他控制器意外地 当做默认的 view了

    注意: 1.创建的xib如果不是想给指定的控制器做view的话,命名就要注意了! 2.最好是不要命名和控制器名字相关的xib. 如下举例说明一下: - (void)touchesBegan:(NSSe ...

  7. 将struts源码导入eclipse

    预制条件和spring源码导入eclipse中一样,下面直接给出导入eclipse的步骤. 步骤: 1. 下载struts相应版本的源码 http://struts.apache.org/downlo ...

  8. oracel数据泵的使用

    1.查看目录,用下面任意一条查询语句即可. select * from dba_directories;         select * from ALL_DIRECTORIES; 2.一般安装好数 ...

  9. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  10. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...