hdu 4991(树状数组优化dp)
生活随笔
收集整理的這篇文章主要介紹了
hdu 4991(树状数组优化dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)Problem Description A numeric sequence of ai is ordered if a1<a2<……<aN. Let the subsequence of the given numeric sequence (a1, a2,……, aN) be any sequence (ai1, ai2,……, aiK), where 1<=i1<i2 <……<iK<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others.
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
Input Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
Output For each case, output answer % 123456789.
Sample Input 3 2 1 1 2 7 3 1 7 3 5 9 4 8
Sample Output 2 12 解題思路:這道題和hdu 5542完全一樣,長度為m的遞增子序列的個數,樹狀數組+dp。 #include<iostream> #include<cstdio> #include<cstring> #include<set> #include<map> using namespace std;const int maxn = 10005; const int mod = 123456789; int n,m; int tree[105][maxn],a[maxn]; set<int> Set; map<int,int> Map;int lowbit(int x) {return x & -x; }void update(int i,int x,int c) {while(x <= n){tree[i][x] = (tree[i][x] + c) % mod;x += lowbit(x);} }int getsum(int i,int x) {int sum = 0;while(x > 0){sum = (sum + tree[i][x]) % mod;x -= lowbit(x);}return sum; }int main() {while(scanf("%d%d",&n,&m)!=EOF){Map.clear();Set.clear();memset(tree,0,sizeof(tree));for(int i = 1; i <= n; i++){scanf("%d",&a[i]);Set.insert(a[i]);}int cnt = 0;for(set<int>::iterator it = Set.begin(); it != Set.end(); it++)Map[*it] = ++cnt;for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){int tmp = 0;if(j == 1) tmp = 1;else tmp = (tmp + getsum(j-1,Map[a[i]]-1)) % mod;update(j,Map[a[i]],tmp);}printf("%d\n",getsum(m,cnt));}return 0; }
總結
以上是生活随笔為你收集整理的hdu 4991(树状数组优化dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA中Git的更新、提交、还原方法
- 下一篇: jeecg 分布式部署附件共享问题(Li