HDU 4990 Ordered Subsequence --数据结构优化DP
题意:给一串数字,问长度为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的更多相关文章
- POJ2533——Longest Ordered Subsequence(简单的DP)
Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...
- hdu 2829 Lawrence(四边形不等式优化dp)
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...
- 数据结构优化dp
本以为自己的dp已经成熟了没想到在优化上面还是欠佳 或者是思路方面优化dp还不太行. 赤壁之战 当然 很有意思的题目描述 大体上是苦肉计吧 .盖黄 ... 题意是 求出长度为m的严格上升子序列的个数 ...
- hdu 3507 Print Article(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- 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 ...
- HDU 3401 Trade(斜率优化dp)
http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HAOI2008 木棍分割 数据结构优化dp+二分答案
很久之前打的题,现在补篇博客 打滚动数组 #E. 木棍分割 Accepted 100 1712 ms 1512 KiB 2019-05-07 17:01:23 Short 不打滚动数组 #419. ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
随机推荐
- java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect
java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect 2014年06月29日 ...
- [Visual Studio Online] 移除Work Item(Feature、Backlog item、Task)
[Visual Studio Online] 移除Work Item(Feature.Backlog item.Task) 移除 项目的开发过程中,使用Visual Studio Online来做Sc ...
- VS 2013 Preview 自定义 SharePoint 2013 列表 之 两个Bug
SharePoint 2013 已RTM了,对于程序员来说又要了解新功能了,同时 VS 2013 也将要 RTM了,两者同时应用定会有不新功能,我们先从 自定义 列表开始. SharePoint 20 ...
- windows下启动mongodb
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#mongodb-as-a-windows-service C:\ ...
- MSCRM 迁移 数据库 服务器
Move the Microsoft Dynamics CRM databases to another SQL Server and SQL Server Reporting Services se ...
- 2015年第7本(英文第6本):纳尼亚传奇I–狮子、女巫、魔衣橱
书名: The Chronicles of Narnia 1 — The Lion, the Witch and the Wardrobe 作者:C.S. Lewis 单词数:4.2万 不重复单词数: ...
- dict和set
#dict和set #dict #Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map #使用键-值(key-value)存储,具有极快的查找速度. #字 ...
- 关于Socket建立长连接遇到的bug信息
下面是本人在Socket连接的开发中遇到的bug总结 1."远程服务器关闭了Socket长连接'的错误信息 2.关于"kCFStreamNetworkServiceTypeVoIP ...
- Java眼中的XML--文件读取--1 应用DOM方式解析XML
初次邂逅XML: 需要解析的XML文件: 这里有两个book子节点. 1.如何进行XML文件解析前的准备工作,另外解析先获取book节点. 这个我后来看懂了: 这个Node的ELEMENT_NODE= ...
- 《慕客网:IOS-动画入门》学习笔记
新建Cocoa Touch Class,语言是swift 然后继续为界面添加一个普通的View Controller,并且添加前面视图的静态table的转向剪头指向这个View Controller, ...