uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
2016-09-23 18:47
351 查看
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217
给你一个长为n的数组,问你有多少个长度严格为m的上升子序列。
dp[i][j]表示以a[i]结尾长为j的上升子序列个数。常规是三个for。
这里用树状数组优化一下,类似前缀和的处理,两个for就好了。
给你一个长为n的数组,问你有多少个长度严格为m的上升子序列。
dp[i][j]表示以a[i]结尾长为j的上升子序列个数。常规是三个for。
这里用树状数组优化一下,类似前缀和的处理,两个for就好了。
//#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; typedef long long LL; typedef pair <int, int> P; const int N = 1e3 + 5; LL dp , mod = 1e9 + 7; int a , b , m, n; LL bit ; void add(int pos, int i, int val) { //上升子序列长度为pos for( ; i <= n; i += (i&-i)) bit[pos][i] = (bit[pos][i] + val) % mod; } LL sum(int pos, int i) { LL s = 0; for( ; i >= 1; i -= (i&-i)) s = (s + bit[pos][i]) % mod; return s; } int main() { int t; scanf("%d", &t); for(int ca = 1; ca <= t; ++ca) { scanf("%d %d", &n, &m); for(int i = 1; i <= n; ++i) { scanf("%d", a + i); b[i] = a[i]; } sort(b + 1, b + n + 1); for(int i = 1; i <= n; ++i) { a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; //离散化 } memset(dp, 0, sizeof(dp)); memset(bit, 0, sizeof(bit)); dp[1][1] = 1; add(1, a[1], 1); for(int i = 2; i <= n; ++i) { dp[i][1] = 1; for(int k = max(m - (n - i), 1); k <= min(i, m); ++k) { //这边可以优化一下 dp[i][k] = (dp[i][k] + sum(k - 1, a[i] - 1)) % mod; //比a[i]小且上升子序列长度为k-1 add(k, a[i], dp[i][k]); } } LL res = 0; for(int i = 1; i <= n; ++i) { res = (res + dp[i][m]) % mod; } printf("Case #%d: %lld\n", ca, res); } return 0; }
相关文章推荐
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
- UESTC 1217 The Battle of Chibi (树状数组 + 离散化 + 动态规划)
- HDU 5542 The Battle of Chibi (离散化+树状数组优化DP)
- UESTC 1217 The Battle of Chibi
- [HDOJ5542]The Battle of Chibi(DP,树状数组)
- CDOJ 1217 The Battle of Chibi【树状数组+dp】
- 2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)
- HDU 5542 The Battle of Chibi——树状数组 + dp
- HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
- The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)
- CDOJ 1217 The Battle of Chibi
- DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
- hdu 5542 The Battle of Chibi
- The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542
- HDU 5542 The Battle of Chibi
- HDU 5542 The Battle of Chibi(树状数组+dp)
- HDU5542 The Battle of Chibi (DP)
- 【模拟】2017 Multi-University Training Contest 1 The Battle of Chibi
- HDU5542 The Battle of Chibi(dp)
- 2015南阳CCPC C - The Battle of Chibi DP