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 }

 

posted @ 2014-09-09 14:08  Naturain  阅读(213)  评论(0编辑  收藏  举报