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
Output
做法: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
相关文章推荐
- android graphic(6)—surfaceflinger和MessageQueue
- ZOJ 3911 Prime Query (线段树+单点更新+区间lazy更新+素数判断计数)
- easyui validatebox 验证
- easyui datagrid 操作
- iOS开发之UIActivityViewController
- ios的手势操作之UIGestureRecognizer浅析
- 使用Hibernate SQLQuery执行原生SQL
- UITableView 分组头视图取消悬浮
- UICollectionView简单使用
- 类似于系统UISegmentedControl的切换控件
- 探索工作流(三)--顺序工作流 Sequence
- SQL写存储过程时报错 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
- 解决AndroidStudio使用build过慢的问题--stackoverflow
- EasyUI datagrid 笔记
- <LeetCode OJ> 52. N-Queens II
- UITabBarController- 标签视图控制器
- iOS Interface Builder:在.xib文件中加载另一个.xib文件
- UITableView 编辑
- UITableView标示图
- UINavigationControl、界面通信