一:关于LIS:

LIS及最长上升子序列,就是对于一个长度为n的数组,上升子序列指的是对于数组中任意的i<j都满足a[i]<a[j]的子序列。

例子:
对于固定的数组,虽然LIS序列不一定唯一,但LIS的长度是唯一的。例如序列2,1,5,3,6,4,6,3的最长上升子序列为1,3,4,6和2,3,4,6
长度均为4
先确定动态规划的状态,这个问题可以用序列某一项作为结尾来作为一个状态。
用dp[i]表示一定以i项为结尾的最长上升子序列。用a[i]表示第i项的值,如果有
j<i 且 a[j] < a[i] ,那么把第i项接在第j项后面构成的子序列长度为:
dp[i] = dp[j] + 1。
要使dp[i]为yii结尾的最长上升子序列,需要枚举所有满足条件的j,所以有:
Ɐ1<=j<i && a[j] < a[i] dp[i] = max(dp[i],dp[j] + 1)

如下图所示:

最长上升子序列长为四。

模板代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int dp[maxn];
int a[maxn];
int main()
{int n;while (cin >> n){for (int i = 1; i <= n; i++)cin >> a[i];int len = 0;for (int i = 1; i <= n; i++){dp[i] = 1;for (int j = 1; j < i; j++){       if (a[i]>a[j])//当前的大于前面的遍历dp[i] = max(dp[i], dp[j] + 1);//在i位置前有多少个最大上升子序列}len = max(len, dp[i]);}cout << len << endl;}return 0;
}

运行结果如下所示:

LIS的二分查找方式 :(优化时间复杂度为O(nlogn)前面的为O(n^2))

方式:是通过lower_bound函数找到对应a[i]最长上升子序列的下标。然后用a[i]去做对应dp下标的覆盖操作。

模板代码如下:

include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int dp[maxn];
int a[maxn];int main(){int n;while (cin >> n){for (int i = 1; i <= n; i++){cin >> a[i];}int len = 1;dp[1] = a[1];for (int i = 2; i <= n; i++){if (dp[len] < a[i]){dp[++len] = a[i];}else{//在a[i]<dp[len]时用a[i]覆盖dp之前的使其尽可能的小保证上升子序列尽可能的长int pos = lower_bound(dp, dp + len, a[i]) - dp;dp[pos]=a[i];//dp[i]=x在x之前包括x有三个最长上升子序列}}cout << len << endl;}return 0;}

二:有如下模板题:

A-Super Jumping! Jumping! Jumping!

**Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
**

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the maximum according to rules, and one line one case.
Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
Sample Output
4
10
3

题目思路:就是先输入一个整数n然后再对这n个数找到最长上升子序列。

AC代码如下:

#include<iostream>
#include<algorithm>
using namespace std;const int maxn = 1010;int a[maxn], dp[maxn];int main(){int n;while (cin >> n&&n!=0){for (int i = 1; i <= n; i++){cin >> a[i];}int ma = 0;for (int i = 1; i <= n; i++){dp[i] = a[i];for (int j = 1; j < i; j++){if (a[i]>a[j]){dp[i] = max(dp[i], dp[j] + a[i]);}}ma = max(ma, dp[i]);}cout << ma<< endl;}return 0;}

C - FatMouse’s Speed

**FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing. **

Input

Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],…, m[n] then it must be the case that

W[m[1]] < W[m[2]] < … < W[m[n]]
and
S[m[1]] > S[m[2]] > … > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output

4
4
5
9
7

本题思路:将输入的第一列按照升序排序,在第二列中找最长下降子序列并返回他们的下标。

AC代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int dp[maxn];struct node{int w;int s;int id;
}p[maxn];bool comp(node a, node b)
{if (a.w != b.w)return a.w <b.w;//升序elsereturn a.s >b.s;
}int main()
{int x = 1;while (scanf("%d%d", &p[x].w, &p[x].s) != EOF){p[x].id = x;x++;}sort(p + 1, p + x + 1, comp);int len = 0;for (int i = x; i >= 1; i--){dp[i] = 1;for (int j = i; j <= x; j++){if (p[i].w<p[j].w&&p[i].s>p[j].s){dp[i] = max(dp[i], dp[j] + 1);}}len = max(len, dp[i]);//最长下降子序列}cout << len << endl;for (int i = 1; i <= x; i++){if (dp[i] == len){cout << p[i].id << endl;len--;}if (len == 0)break;}return 0;
}

关于LIS(普通方式及二分查找方式)相关推荐

  1. 二分查找(5种方式实现二分查找),栈

     1.5种方式实现二分查找,案例结构: halfFind.h #ifndef _HALFFIND_ #define _HALFFIND_ /**************************** ...

  2. 二分查找算法(递归与非递归两种方式)

    首先说说二分查找法. 二分查找法是对一组有序的数字中进行查找,传递相应的数据,进行比较查找到与原数据相同的数据,查找到了返回1,失败返回对应的数组下标. 采用非递归方式完成二分查找法.java代码如下 ...

  3. 【转】刨根究底正则表达式(2):文本查找方式的演化历史

    上一篇文章讲到,从根源上来讲,正则表达式是为了解决文本的查找问题(也称为匹配问题)而诞生的.不过,文本查找方式的历史,要远早于正则表达式. 那么,在正则表达式出现之前,文本查找方式经历了什么演化过程呢 ...

  4. 算法图解学习笔记01:二分查找大O表示法

    二分查找 二分查找又称折半查找,其输入的必须是有序的元素列表.二分查找的基本思想是将n个元素分成大致相等的两部分,取a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止:如果x<a[ ...

  5. 二分查找递归与非递归的时间比较_我们说一说Python的查找算法!

    相信大家在面试开发岗和算法岗时,评委最喜欢问的就是:您能给我说一下查找和排序算法有哪些?今天咱们就说一说Python中最常用的查找算法,下期我们再推出排序算法. 首先要明白查找是查什么?我们希望能给定 ...

  6. 二分查找算法详细汇总

    二分查找算法详细汇总 文章目录 二分查找算法详细汇总 二分查找算法介绍模板 算法思想 举例说明 模板实现 (1)递归方式实现 (2)非递归方式实现 二分查找问题相关难点问题1[前缀和 + 二分查找 + ...

  7. 二分查找 Binary Search

    明确循环不变量:变量的值可能在变化,但是它的含义是不变的 https://baike.baidu.com/item/循环不变量/8353186?fr=aladdin 如何写出正确的程序? 明确变量的含 ...

  8. LeetCode力扣刷题——居合斩!二分查找

    二分查找 一.算法解释         二分查找也常被称为二分法或者折半查找,每次查找时通过将待查找区间分成两部分并只取 一部分继续查找,将查找的复杂度大大减少.对于一个长度为 O ( n ) 的数组 ...

  9. 二分查找(Binary Search)需要注意的问题,以及在数据库内核中的实现

    问题背景 今年的实习生招聘考试,我出了一道二分查找(Binary Search)的题目.题目大意如下: 给定一个升序排列的自然数数组,数组中包含重复数字,例如:[1,2,2,3,4,4,4,5,6,7 ...

最新文章

  1. Android实战简易教程-第三十四枪(基于ViewPager和FragmentPagerAdapter实现滑动通用Tab)...
  2. jQuery中ajax的4种常用请求方式
  3. Scan Chain的原理与实现(实践) - Compression Flow
  4. docker部署django项目、mysql主从搭建、django实现读写分离
  5. 央行数字货币研究所悄然挂牌 工作人员:已有一段时间
  6. 电脑开机3秒就重启循环_电脑修好后客户不愿支付上门费,行,那电脑开机60秒自动关机吧!...
  7. 设计没灵感,哪些网站可以参考?
  8. java调用python脚本_调用Python写vb的脚本方法
  9. python 数字类型判断_Python中 各种数字类型的判别(numerica, digital, decimal)
  10. 蓝桥杯2015年第六届C/C++省赛B组第一题-奖券数目
  11. 实体词典 情感词典_基于情感词典的情感分析
  12. 基于springboot在线租车管理系统
  13. 英文翻译软件哪个好?不能错过的有这几个。
  14. 开发撞墙之奇怪的需求:经纬度带符号转换
  15. /deep/的使用与导致样式失效问题处理
  16. docker部署time machine服务
  17. svg中marker元素的理解
  18. jmeter中变量的作用范围_血糖范围内达标时间在糖尿病管理中的作用
  19. 查看linux操作系统版本--实用
  20. C语言之struct

热门文章

  1. 用 Python 开发游戏——Python井字游戏
  2. 2022-04-08 Android app小电筒(闪光灯)Torch 测试demo,获取手电筒状态。
  3. 正则表达式与corn表达式
  4. SAP UI5 应用的全局配置(Global Configuration) 的设计和使用试读版
  5. 对每项物品,找出最贵价格的物品的经销商
  6. 把英文和数字组合分离并取出其中的数字和英文
  7. (附源码)spring boot高校机房自动排课系统 毕业设计211004
  8. ubuntu安装使用Beyond Compare4
  9. js插件获取当前所在的省市
  10. jsp调用css文件