HDU6197 array array array 最长上升子序列(模板题)
来源:互联网 发布:ubuntu 16.10安装lamp 编辑:程序博客网 时间:2024/06/07 04:42
题目链接:点击打开链接
题目思路:求出LIS和LDS(最长下降子序列,自己编的名字:),如果 LIS+k>=n或者LDS+k>=n 则输出 A is a magic array. 否则输出A is not a magic array.
题目的意思有点绕,其实就是如果去掉k个元素使得剩余的不是递增序列或者不是递减序列 等价于 去掉k个元素后剩余的元素是递减序列或者递增序列。这样就懂了吧。
AC代码:
/*2017年9月10日20:44:38HDU6197最长上升子序列 签到 AC */ #include <iostream>#include <map>#include <set>#include <string>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector> using namespace std;const int maxn = 100010;int n;int a[maxn],b[maxn];int dp[maxn];int lis(int *a){ memset(dp, 0, sizeof(int)*n); int len = 1; dp[0] = a[0]; for (int i = 1; i < n; ++i) { int pos = lower_bound(dp, dp + len, a[i]) - dp; dp[pos] = a[i]; len = max(len, pos + 1); } return len;}int main(){int t;scanf("%d",&t);while(t--){int k;scanf("%d%d",&n,&k);//正着存 for(int i=0;i<n;i++){scanf("%d",&a[i]);}//倒着存,求一遍即为最长递减序列 for(int i=0;i<n;i++){b[i]=a[n-1-i];}if(lis(a)+k>=n||lis(b)+k>=n) printf("A is a magic array.\n");else printf("A is not a magic array.\n");} return 0;}
阅读全文
0 0
- HDU6197 array array array 最长上升子序列(模板题)
- array array array (最长上升子序列变形)
- hdu 6197 array array array (LIS,最长上升子序列模板题)
- 【2017沈阳网络赛】1004 hdu6197 array array array 最长不下降子序列
- HDU 6197 array array array(最长上升或下降子序列)
- hdu6197array array array(最长上升子序列二分版)
- Hdu6197 array array array
- HDU6197-array array array
- hdu6197(最长上升子序列)
- hdu6197 array array array【英文题】
- HDU 6197 array array array (2017沈阳网赛- 最长上升子序列)
- hdu-6197 array array array(最长上升子序列(LIS)长度的O(nlogn)算法)
- hdu5532Almost Sorted Array 最长递增子序列模板水题
- hdu 5532 Almost Sorted Array 最长上升子序列
- hdu5532 Almost Sorted Array--最长上升子序列
- HDU5532 Almost Sorted Array(最长不上升子序列)
- hdu 5532 Almost Sorted Array(最长上升(不下降)子序列和最长下降(不上升)子序列)
- hdu6197 array array array LIS
- [Cracking the Coding Interview] Chapter 2
- [Cracking the Coding Interview] Chapter 3
- QWT编译、配置、使用(Qt Creator)
- 『默哀』你的梦或许因为这个新闻而碎了【用你的程序语言 抛出一行异常】
- [LeetCode] Convert Sorted Array to Binary Search Tree
- HDU6197 array array array 最长上升子序列(模板题)
- [LeetCode] Convert Sorted List to Binary Search Tree
- JS中script标签位置对页面的影响
- [LeetCode] Next Permutation
- poj 3321(dfs序&&树状数组)
- [LeetCode] Word Ladder
- [WikiOI] 1.1.1 最小数和最大数
- 【BZOJ4872】【SHOI2017】分手是祝愿 期望DP
- [WikiOI] 1.1.2 求和