类似于最长不降子序列。
找到最长的大小相间的子序列。
dp[k] 表示一个结构体,以k号结尾的最长zig-zag子序列长度,和期待下一个数是大还是小。

struct node{int len, state; //state, 0表示下一个数必须大,1表示必须小,2表示都可以
}

状态转移方程
dp[k].len = max( num[k]能接到dp[i]后面 ? dp[i].len+1 : 1 ), 0<=i<=k-1
边界

dp[0].len = 1;
dp[0].state = 2;

处理顺序
从左往右

#include <cstdio>struct node{int len, state;
};int main(){int n, ans = 1;scanf("%d", &n);int num[n];for(int i=0; i<n; i++) scanf("%d", &num[i]);node dp[n];dp[0].len = 1;dp[0].state = 2;for(int i=1; i<n; i++){dp[i].len = -1;for(int j=0; j<i; j++){node temp;if(dp[j].state==0){if(num[i]>num[j]){temp.len = dp[j].len+1;temp.state = 1;}else{temp.len = 1;temp.state = 2;}}else if(dp[j].state==1){if(num[i]<num[j]){temp.len = dp[j].len+1;temp.state = 0;}else{temp.len = 1;temp.state = 2;}}else{if(num[i]>num[j]){temp.len = dp[j].len + 1;temp.state = 1;}else if(num[i]<num[j]){temp.len = dp[j].len + 1;temp.state = 0;}else{temp.len = 1;temp.state = 2;}}if(temp.len > dp[i].len) dp[i] = temp;}if(dp[i].len > ans) ans = dp[i].len;}printf("%d", ans);return 0;
}

The longest zig-zag subsequence相关推荐

  1. LintCode Longest Increasing Continuous Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/ 题目: Give an in ...

  2. 674 Longest Continuous Increasing Subsequence(每日一题)

    674:Longest Continuous Increasing Subsequence 每日一题的第一天 解题思路 双重循环 class Solution {public int findLeng ...

  3. LeetCode 674. Longest Continuous Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...

  4. C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...

  5. 674. Longest Continuous Increasing Subsequence最长连续递增子数组

    [抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...

  6. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  8. LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  9. LeetCode 674. Longest Continuous Increasing Subsequence--python,Java,C++解法

    此题链接:Longest Continuous Increasing Subsequence - LeetCode Given an unsorted array of integers, find ...

  10. 九章算法班ladder题目梳理

    1 - 从strStr谈面试技巧与代码风格 13.字符串查找 如果target在source中,返回起始下标,否则返回-1 要点:该题O(mn)可过,两层循环即可. class Solution:de ...

最新文章

  1. python安装出错0xc00006b_Python安装时我遇到的一些问题
  2. SqlServer 0和空字符串''等价?-----类型的隐式转换问题
  3. 《我的侠客》主策划何铮:如何打出武侠游戏的新招
  4. mysql as用法_Spring Data R2DBC响应式操作MySQL
  5. EBS MOAC深入研究(转)
  6. UIViewController内存管理
  7. Q140:PBRT-V3,各种渲染算法(Integrator,积分器)汇总
  8. SQL调优技巧:统计信息(文末福利)
  9. pythonsuper多重继承_解决python super()调用多重继承函数的问题
  10. 小程序 实名信息_公告丨DUNK PLUS 小程序实名认证开启
  11. 麻雀算法(SSA)优化的BP神经网络预测,SSA-BP回归预测,多输入单输出模型。
  12. 吐槽一下程序员职场那些令人迷惑的行为
  13. 2022R2移动式压力容器充装考题模拟考试平台操作
  14. 记录使用4G模块SIM7600CE的一些问题
  15. 手势识别原来还可以这样控制电脑,你知道怎么做到的吗?
  16. 线性规划模型应用的2个案例
  17. AQS是什么?都是怎么用的?
  18. saas系统的微服务器,基于微服务架构的SaaS服务应用的研究与实践
  19. 鲁班H5页面生成工具
  20. 回顾我在中兴通讯的五年(一)

热门文章

  1. 已更新或删除的行值要么不能使该行成为唯一行
  2. 排列序列(第k个排列)
  3. 67在C51语言表示16进制数,计算机真题
  4. 怎样使用计算机的桌面助手,360桌面助手怎么用
  5. ESD防护的4种静电保护设计
  6. java计算机毕业设计基于安卓Android的社交app-社会交友app
  7. 为什么《请回答1988》能被称为神剧
  8. 今有雉兔同笼c语言编程,Openjudge-计算概论(A)-鸡兔同笼
  9. apple 的 app id
  10. 游戏框架 核心科技与面试精粹 (樊松阳 著)