您的位置:首页 > 产品设计 > UI/UE

codefoces 597C - Subsequences

2016-01-26 13:00 344 查看
题意:求出上升子序列为k的数量

做法:dp[0][i][j]:以第i个数为结尾的序列长度为j的数量,dp[0][i][j]=sum(dp[0][ii][j-1]),a[ii]<a[i] and ii<i,这里就是算前缀和,用k棵树状数组优化

dp[1][i][j]:前i个数中,序列长度为j的数量,dp[1][i][j]=dp[1][i-1][j]+dp[0][i][j]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll bit[100010][12],dp[2][100010][12];
void update(int x,int y,ll val)
{
for(int i=x;i<=100000;i+=i&-i)
bit[i][y]+=val;
}
ll seek(int x,int y)
{
ll ans=0;
for(int i=x;i>0;i-=i&-i)
ans+=bit[i][y];
return ans;
}
int main()
{
// freopen("in","r",stdin);
int n,m;
cin>>n>>m;
m++;
for(int i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
dp[0][i][1]=1;
for(int j=2;j<=m;j++)
dp[0][i][j]=seek(t,j-1);
for(int j=1;j<=m;j++)
update(t,j,dp[0][i][j]);
for(int j=1;j<=m;j++)
dp[1][i][j]=dp[1][i-1][j]+dp[0][i][j];
}
cout<<dp[1]
[m];
}


time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

For the given sequence with n different elements find the number of increasing subsequences with
k + 1 elements. It is guaranteed that the answer is not greater than
8·1018.

Input
First line contain two integer values n and
k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.

Next n lines contains one integer
ai (1 ≤ ai ≤ n) each — elements of sequence. All values
ai are different.

Output
Print one integer — the answer to the problem.

Sample test(s)

Input
5 2
1
2
3
5
4


Output
7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: