例子:
对于固定的数组,虽然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 dp[i] = dp[j] + 1。
要使dp[i]为yii结尾的最长上升子序列,需要枚举所有满足条件的j,所以有:
Ɐ1<=j
#include
#include
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;
}
include<iostream>
#include
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]
int pos = lower_bound(dp, dp + len, a[i]) - dp;
dp[pos]=a[i];//dp[i]=x在x之前包括x有三个最长上升子序列
}
}
cout << len << endl;
}
return 0;
}
**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
#include
#include
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;
}
**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 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.
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
4
4
5
9
7
#include
#include
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;
}