LightOJ 1085 All Possible Increasing Subsequences (DP&离散化&树状数组)
2014-03-25 21:28
399 查看
http://lightoj.com/volume_showproblem.php?problem=1085
先说个很快的方法——二维思考,从右下往左上离散化:
然后是普遍的从左往右离散化的方法,但是太慢,而且还耗内存:
先说个很快的方法——二维思考,从右下往左上离散化:
/*0.364s,2860KB*/ #include<bits/stdc++.h> using namespace std; const int mx = 100005; const int mod = 1000000007; int tree[mx], a[mx], dis[mx], n; ///从右下往左上“看” bool cmp(int x, int y) { if (a[x] != a[y]) return a[x] < a[y]; return x > y; } void add(int pos, int x) { for (; pos <= n; pos += pos & -pos) tree[pos] = (tree[pos] + x) % mod; } int sum(int pos) { int res = 0; for (; pos; pos -= pos & -pos) res = (res + tree[pos]) % mod; return res; } int main() { int t, cas, i; scanf("%d", &t); for (cas = 1; cas <= t; ++cas) { scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", &a[i]), dis[i] = i; sort(dis + 1, dis + n + 1, cmp); memset(tree, 0, sizeof(tree)); for (i = 1; i <= n; i++) add(dis[i], sum(dis[i]) + 1); printf("Case %d: %d\n", cas, sum(n)); } return 0; }
然后是普遍的从左往右离散化的方法,但是太慢,而且还耗内存:
/*1.236s,7088KB*/ #include<bits/stdc++.h> using namespace std; const int mx = 100005; const int mod = 1000000007; int tree[mx], a[mx], n; map<int, int> m; void add(int pos, int x) { for (; pos <= n; pos += pos & -pos) tree[pos] = (tree[pos] + x) % mod; } int sum(int pos) { int res = 0; for (; pos; pos -= pos & -pos) res = (res + tree[pos]) % mod; return res; } int main() { int t, cas, i, cnt, pos; map<int, int>::iterator it; scanf("%d", &t); for (cas = 1; cas <= t; ++cas) { scanf("%d", &n); m.clear(); for (i = 0; i < n; ++i) scanf("%d", &a[i]), m[a[i]]; cnt = 0; for (it = m.begin(); it != m.end(); ++it) it->second = ++cnt; memset(tree, 0, sizeof(tree)); for (i = 0; i < n; ++i) pos = m[a[i]], add(pos, sum(pos - 1) + 1); printf("Case %d: %d\n", cas, sum(cnt)); } return 0; }
相关文章推荐
- lightoj 1085 - All Possible Increasing Subsequences 【树状数组优化dp】
- lightoj1085 - All Possible Increasing Subsequences(树状数组)
- [树状数组]LightOJ 1085 - All Possible Increasing Subsequences
- LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)
- 1085 - All Possible Increasing Subsequences[树状数组]
- Lightoj 1085 All Possible Increasing Subsequences (树状数组+DP)
- LightOJ 1085 - All Possible Increasing Subsequences(DP + 线段树 + 离散)
- LightOJ-1085-树状数组,离散化,dp
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
- HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)
- Light OJ 1085 - All Possible Increasing Subsequences
- lightoj 1085【离散化+树状数组】
- lightoj 1085【离散化+树状数组】
- hdu2227 && hdu3450 【树状数组优化dp】
- 【树状数组 + 离散化 + DP】 HDU 5542
- CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和
- Find the nondecreasing subsequences(数状数组+离散化+dp)
- LightOJ 1112 Curious Robin Hood && LightOJ 1266 Points in Rectangle(树状数组)
- hdoj 3450 Counting Sequences 【离散化 + 树状数组优化dp】
- 【Codeforces 597C】【DP 树状数组优化】Subsequences 【n个不同数,长度为k+1的LIS数】