Hdu--4991(树状数组,离散化,DP)

2014-09-09 14:05:21

题目链接

思路:用树状数组来优化DP过程,详情见Bestcoder题解,本来用以前学过的离散化来写。。结果搞不定,于是重新学了个离散化:推荐博客

 

  离散化思路:用b[]数组用来做a[]数组的副本,然后就是unique,以及lower_bound函数的调用了。

 1 /*************************************************************************
 2     > File Name: hdu4991.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Mon 08 Sep 2014 10:47:42 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 const int INF = 1 << 29;
19 const int maxn = 10010;
20 const ll mod = 123456789;
21 
22 int a[maxn],b[maxn],n,m;
23 ll c[105][maxn];
24 
25 int Lowbit(int x){
26     return x & (-x);
27 }
28 void Update(int x,int d,int k){
29     while(x <= maxn){
30         c[k][x] = (c[k][x] + d) % mod;
31         x += Lowbit(x);
32     }
33 }
34 ll Getsum(int x,int k){
35     ll res = 0;
36     while(x){
37         res = (res + c[k][x]) % mod;
38         x -= Lowbit(x);
39     }
40     return res;
41 }
42 
43 int main(){
44     while(scanf("%d%d",&n,&m) != EOF){
45         memset(c,0,sizeof(c));
46         for(int i = 1; i <= n; ++i){
47             scanf("%d",a + i);
48             b[i] = a[i];
49         }
50         sort(b + 1,b + n + 1);
51         int size = unique(b + 1,b + n + 1) - b - 1;
52         for(int i = 1; i <= n; ++i)
53             a[i] = lower_bound(b + 1,b + size + 1,a[i]) - b;
54         ll tmp;
55         for(int i = 1; i <= n; ++i){
56             Update(a[i],1,1);
57             for(int j = 1; j < m; ++j){
58                 tmp = Getsum(a[i] - 1,j);
59                 if(!tmp) break;
60                 Update(a[i],tmp,j + 1);
61             }
62         }
63         printf("%lld\n",Getsum(maxn,m) % mod);
64     }
65     return 0;
66 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3962557.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值