hust校赛 f题 The tree of hust(lis 变形)
2015-05-19 21:12
330 查看
题目大意是给出一段数字序列,可以忽略一次一段连续的序列,求忽略后的最长连续上升子序列
思路是dp,用end数组记录以当前元素作为结尾的最长连续上升序列的元素个数,那么不难得到状态转移方程为
dp(i) = max(dp(i - 1), max( end[k] ) ) + 1
代码如下:
思路是dp,用end数组记录以当前元素作为结尾的最长连续上升序列的元素个数,那么不难得到状态转移方程为
dp(i) = max(dp(i - 1), max( end[k] ) ) + 1
代码如下:
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> using namespace std; #define LL long long const int maxn = 200009; const int INF = 1000000007; int a[maxn], enda[maxn], d[maxn], g[maxn]; //enda数组记录以当前元素结尾的最长上升连续序列的长度 int main() { int startt = clock(); freopen("input.txt", "r", stdin); int t, kase = 0; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); a[0] = 0; enda[0] = 0; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); if(a[i] > a[i - 1]) enda[i] = enda[i - 1] + 1; else enda[i] = 1; // printf("%d\n", enda[i]); } int ans = 0; for(int i = 1; i <= n; i++) g[i] = INF; //lis的变形,g[i]表示enda的值为i的最小元素值 for(int i = 1; i <= n; i++) { int k = lower_bound(g + 1, g + n + 1, a[i]) - g - 1; if(a[i] > a[i - 1]) d[i] = max(d[i - 1], k) + 1; else d[i] = k + 1; ans = max(ans, d[i]); g[enda[i]] = min(g[enda[i]], a[i]); // printf("%d\n", d[i]); } printf("Case #%d: %d\n", ++kase, ans); } int endt = clock(); printf("%d\n", endt - startt); return 0; }
相关文章推荐
- UVa 437 (变形的LIS) The Tower of Babylon
- UVA 437 The Tower of Babylon【LIS变形+O(n2)算法】
- Given a binary tree, return the postorder traversal of its nodes' values.
- hdu 3999 The order of a Tree
- hust1384---The value of F[n]
- The diameter of a binary tree [No. 40]
- uva 437 The Tower of Babylon (DAG_变形 ,dp)
- hdu3999 the order of a tree - BST二叉搜索树的前序遍历
- RadTreeView: How to Wrapping the text of tree nodes
- hdu 3999 The order of a Tree
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- XTU Binary Search Tree(LIS变形)
- hdu 3999 The order of a Tree
- New study shakes the roots of the dinosaur family tree
- Given a tree, find the node with the minimum sum of distances to other nodes
- 深入解读AlphaGo,Nature-2016:Mastering the game of Go with deep neural networks and tree search
- Given a binary tree, return the preorder traversal of its nodes' values.
- HDU3999-The order of a Tree
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- The height of binary tree