题目链接:

Ordered Subsequence

Time Limit: 4000/2000 MS (Java/Others)  

  Memory Limit: 32768/32768 K (Java/Others)

Problem Description
 
A numeric sequence of ai is ordered if a1<a2<……<aN. Let the subsequence of the given numeric sequence (a1, a2,……, aN) be any sequence (ai1, ai2,……, aiK), where 1<=i1<i2 <……<iK<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others.

Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.

 
Input
 
Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
 
Output
 
For each case, output answer % 123456789.
 
Sample Input
3 2
1 1 2
7 3
1 7 3 5 9 4 8
 
Sample Output
2
12
 
题意:
 
求长为n的数组中的长度为m的单调递增子序列的个数;
 
思路:
 
跟又一次的CF一样,只不过这题还要离散化;
dp[i][j]表示以第j个结尾长为i的子序列的个数;
 
 
AC代码:
 
/*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 ;
}
 

最新文章

  1. Eclipse安装svn插件的几种方式
  2. 01.线性表 ArrayList
  3. linux服务器加硬盘扩容
  4. 转自coolshell--vim的基本操作
  5. Java学习笔记(二)&mdash;&mdash;变量与常量
  6. Java再学习——sleep(), wait(), notify(), notifyAll()
  7. POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)
  8. hdu1869 六度分离(Floyd)
  9. SQLPlus命令
  10. Mr. Frog’s Game
  11. c++实验二
  12. react-native命令行打包APK报错
  13. Spring事务管理transactionManager
  14. 一、tars简单介绍 二、tars 安装部署资料准备
  15. shit iview docs &amp; i-radio bug
  16. ******十三 ******、软设笔记【操作系统】-磁盘管理、虚设备与SPOOLing系统
  17. 【转】Vim速查表-帮你提高N倍效率
  18. 怎么把html页面中共用的底部代码做成共享模块
  19. PHP自学之路---雇员管理系统(1)
  20. mybatis由浅入深day01_5.3 Mapper动态代理方法

热门文章

  1. Xcode: This device is no longer connected error
  2. 算法之美--3.2.2 MP算法
  3. NYOJ 38 布线问题_(解法1 Kruskal算法)
  4. 【环境配置】Linux的经常使用命令
  5. python(28)- 面向对象练习Ⅱ
  6. MySQL的timeout那点事
  7. C# 将cookie写入WebBrowser
  8. c和c++的输入输出
  9. 系统安全-SElinux
  10. 详谈kubernetes滚动更新-1