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

一:关于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;//升序
	else
	return 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;
}
					

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值