CodeForces 597C 树状数组 dp
2017-01-16 10:29
330 查看
CodeForces 597C
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll maxn = 1e5;
ll d[maxn+10];
ll dp[15][maxn+10];
ll a[maxn+10];
ll n,k;
ll lowbit(ll x){
return x&(-x);
}
ll updata(ll x,ll k){
while(x <= n){
d[x] += k;
x += lowbit(x);
}
}
ll getSum(ll x){
// if(x == 0) return 0;
ll ans = 0;
while(x > 0){
ans += d[x];
x -= lowbit(x);
}
return ans;
}
// dp[长度][结尾的数] = t sum(1,结尾的数-1)dp[长度-1][t] // 树状数组维护这个前n项和
int main(){
while(scanf("%I64d%I64d",&n,&k) != EOF){
memset(dp,0,sizeof(dp));
for(int i = 1;i <= n;++i) scanf("%I64d",&a[i]);
for(int i = 1;i <= n;++i) dp[1][i] = 1;
for(int i = 2;i <= k+1;++i){
memset(d,0,sizeof(d));
for(int j = 1;j <= n;++j){
dp[i][a[j]] += getSum(a[j]-1);
updata(a[j],dp[i-1][a[j]]);
}
}
ll ans = 0;
for(int i = 1;i <= n;++i) ans += dp[k+1][i];
printf("%I64d\n",ans);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll maxn = 1e5;
ll d[maxn+10];
ll dp[15][maxn+10];
ll a[maxn+10];
ll n,k;
ll lowbit(ll x){
return x&(-x);
}
ll updata(ll x,ll k){
while(x <= n){
d[x] += k;
x += lowbit(x);
}
}
ll getSum(ll x){
// if(x == 0) return 0;
ll ans = 0;
while(x > 0){
ans += d[x];
x -= lowbit(x);
}
return ans;
}
// dp[长度][结尾的数] = t sum(1,结尾的数-1)dp[长度-1][t] // 树状数组维护这个前n项和
int main(){
while(scanf("%I64d%I64d",&n,&k) != EOF){
memset(dp,0,sizeof(dp));
for(int i = 1;i <= n;++i) scanf("%I64d",&a[i]);
for(int i = 1;i <= n;++i) dp[1][i] = 1;
for(int i = 2;i <= k+1;++i){
memset(d,0,sizeof(d));
for(int j = 1;j <= n;++j){
dp[i][a[j]] += getSum(a[j]-1);
updata(a[j],dp[i-1][a[j]]);
}
}
ll ans = 0;
for(int i = 1;i <= n;++i) ans += dp[k+1][i];
printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- Codeforces 597C(树状数组 + dp)
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
- Codeforces 597C Subsequences 【树状数组优化DP】
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- CodeForces 602E【概率DP】【树状数组优化】
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- codeforces-597C-Subsequences【树状数组】
- George and Cards - CodeForces 387E 树状数组
- codeforces 191 E (树状数组+二分)
- codeforces 362 C. Insertion Sort(树状数组,逆序数)
- HDU - 4991(树状数组优化 dp)
- Codeforces-Round 174(Cows and Sequence)(树状数组、高校算法)
- Codeforces 383C . Propagating tree【树状数组,dfs】
- POJ 2029 Get Many Persimmon Trees (二维树状数组 or DP)
- hdu 4991 Ordered Subsequence(树状数组优化DP)
- 【HDU4991】dp 树状数组优化
- codeforces 459D Pashmak and Parmida's problem 树状数组
- Codeforces 459D Pashmak and Parmida's problem(树状数组)
- Codeforces 383C Propagating tree(树状数组)