hdu-4991 Ordered Subsequence(dp+树状数组)
题目链接:
Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
/*4991 655MS 9664K 1701 B G++ 2014300227*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=;
int n,m;
ll sum[N],dp[][N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,ll num)
{
while(x<=n)
{
sum[x]+=num;
sum[x]%=mod;
x+=lowbit(x);
}
}
ll query(int x)
{
ll s=;
while(x>)
{
s+=sum[x];
s%=mod;
x-=lowbit(x);
}
return s;
}
struct node
{
int num,pos,c,d;
};
node po[N];
int cmp1(node x,node y)
{
if(x.num==y.num)return x.pos<y.pos;
return x.num<y.num;
}
int cmp2(node x,node y)
{
return x.pos<y.pos;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)scanf("%d",&po[i].num),po[i].pos=i;
sort(po+,po+n+,cmp1);
po[].num=-;
for(int i=;i<=n;i++)
{
if(po[i].num==po[i-].num)
{
po[i].c=po[i-].c;
}
else po[i].c=i;//po[i].c表示第一个跟po[i].num相同的数的位置;
po[i].d=i;//表示po[i]插入时的位置;
}
sort(po+,po+n+,cmp2);
for(int i=;i<=n;i++)
{
dp[][i]=;
update(po[i].d,);
}
for(int i=;i<=m;i++)
{
memset(sum,,sizeof(sum));
for(int j=;j<=n;j++)
{
if(po[j].c>)
dp[i][j]=query(po[j].c-);//转移方程;
else dp[i][j]=;
update(po[j].d,dp[i-][j]);//把dp[i-1][j]更新上去;
}
}
ll ans=;
for(int i=;i<=n;i++)
{
ans+=dp[m][i];
ans%=mod;
}
printf("%lld\n",ans);
}
return ;
}
hdu-4991 Ordered Subsequence(dp+树状数组)的更多相关文章
- HDU4991 Ordered Subsequence (树状数组优化DP)
dp[i][j]表示以a[i]结尾的长度为j的上升子序列个数. 方程:dp[i][j]=sum(dp[k][j-1]),a[k]<a[i],1<=k<i. 求解目标:sum(dp[k ...
- HDU 2836 Traversal 简单DP + 树状数组
题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个. dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( he ...
- HDU 5489 Removed Interval DP 树状数组
题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
随机推荐
- Hierarchical data in postgres
https://coderwall.com/p/whf3-a/hierarchical-data-in-postgres --------------------------------------- ...
- 视图交互--表视图(UITableView)的cell交互析略
在表视图UITableView的cell上经常有一些交互,根据项目开发中的情况,需要对此进行一些规范.总结出了几种交互方法,这些方法在其他视图的交互上同样可以适用.用一个简单的例子来举例说明一下,其他 ...
- CMake - boost - 可运行程序 - 静态库
CMake - boost 最后更新日期:2014-04-25by kagula 阅读前提:<CMake入门(二)>.Linux的基本操作 环境: Windows 8.1 64bit英文版 ...
- Cursor类用法:
Cursor类用法: http://www.2cto.com/kf/201109/103163.html Ctrl+Shift+G 查找类.方法和属性的引用.这是一个非常实用的快捷键,例如 ...
- MySQL 下优化SQL语句的一些经验
http://java-guru.iteye.com/blog/143377
- Python+Selenium ----unittest单元测试框架
unittest是一个单元测试框架,是Python编程的单元测试框架.有时候,也做叫做“PyUnit”,是Junit的Python语言版本.这里了解下,Junit是Java语言的单元测试框架,Java ...
- leetCode(40):Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Cadence SPB 16. 6 安装步骤
1.首先下载Cadence Allegro SPB orCAD16. 6 安装包,单击我,下载之后运行其中的setup.exe,然后先安装第一项License Manager
- HDU-4930 Fighting the Landlords 多校训练赛斗地主
仅仅须要推断一个回合就能够了,枚举推断能够一次出全然部牌或者大过对面的牌的可能,注意的是4张同样的牌带两张牌的话是能够被炸弹炸的. #include <iostream> #include ...
- MonoTouch.Dialog简介
新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...