Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 214 Accepted Submission(s): 109
Problem Description
A numeric sequence of a
i is ordered if a
1<a
2<……<a
N. Let the subsequence of the given numeric sequence (a
1, a
2,……, a
N) be any sequence (a
i1, a
i2,……, a
iK), where 1<=i
1<i
2 <……<i
K<=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.
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
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
数据的离散化+树状数组
第一次了解离散化点击打开链接
//562ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=10000+100;
const int mod=123456789;
int a[maxn+10],b[maxn+10];
int dp[110][maxn+10];
int low(int k)
{
return (k&-k);
}
int getsum(int k,int u)
{
int ans=0;
while(k>0)
{
ans=(ans+dp[u][k])%mod;
k-=low(k);
}
return ans;
}
void update(int u,int k,int v)
{
while(k<maxn)
{
dp[u][k]=(dp[u][k]+v)%mod;
k+=low(k);
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
if(n<m)
{
printf("0\n");
continue;
}
sort(b+1,b+n+1);//数据的离散化
int size=unique(b+1,b+n+1)-b-1;//离散后的长度,去重
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+n+1,a[i])-b;//离散后的值
update(1,a[1],1);
// printf("jsd\n");
for(int i=2;i<=n;i++)
{
for(int j=m;j>=2;j--)//枚举前面所有可能的长度
{
int sum=getsum(a[i]-1,j-1);
update(j,a[i],sum);
}
update(1,a[i],1);
}
int ans=getsum(size,m);
printf("%d\n",ans);
}
return 0;
}

本文介绍了一种使用离散化和树状数组解决特定问题的方法:即计算一个给定数值序列中长度为m的所有递增子序列的数量。通过示例代码详细展示了如何实现这一算法,并解释了关键步骤。
445

被折叠的 条评论
为什么被折叠?



