(最长上升子序列)的问题,通过dp也可以很有效率地求解。我们首先来建立一下递推关系。
定义dp[i]= 以a[i]为末尾的最长上升子序列的长度。
以a[i]结尾的上升子序列可定义为:
1、只包含a[i]的子序列
2、在满足j<i并且a[j]<a[i]的以a[j]为结尾的上升子列末尾,追加上a[i]后得到的子序列
这二者之一。这样就能得到以下的递推关系:
dp[i] = max{1 , dp[j] +1(满足j<i且a[j]<a[i]) }
使用这一递推公式可以在O(n²)时间内解决这一问题。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a[2000];
int dp[2000];
int main()
{
int n;
cin>>n; //输入序列的规模n
for (int i = 0; i < n; i++)
scanf("%d",&a[i]); //输入a[i]
memset(dp,0,sizeof(dp));
int res = 0;
for(int i = 0; i < n; i++)
{
dp[i] = 1;
for(int j = 0; j < i; j++)
{
if(a[j] < a[i]) //如果把 "<" 改成 ">" ,就得到最长下降子序列的求法。
{
dp[i] = max(dp[i], dp[j] + 1);
}
}
res = max(res, dp[i]);
}
cout<<res<<endl;
}
此外还可以定义其他的关系。前面我们利用dp求取针对最末位的元素的最长的子序列。如果子序列的长度相同,那么最末位的元素较小的在之后会更有优势,所以我们再反过来用dp针对相同长度情况下最小的末尾元素进行求解。
dp[i] = 长度为i+1的上升子序列中末尾元素的最小值(不存在的话就是INF)
我们来看看如何用dp来更新这个数组。
最开始全部dp[i]的值都初始化为INF,然后由前到后逐个考虑数列的元素,对于每个a[j],如果i=0或者dp[i-1]<a[j]的话,就用dp[i] = min(dp[i],a[j])进行更新。最终找出使得dp[i]<INF的最大的i+1就是结果了。这个DP直接实现的话,能够与前面的方法一样,在O(n²)的时间内给出结果,但这一算法还可以进一步优化。首先dp数列中除INF之外,都是单调递增的,所以可以知道对于每个a[j]最多只需要一次更新。对于这次更新究竟应该在什么位置,不必逐个遍历,可以利用二分搜索,这样就可以在O(nlogn)的时间内求出结果。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int INF =0x3f3f3f3f;
int a[2000];
int dp[2000];
int main()
{
int n;
cin>>n;
for (int i = 0; i < n; i++)
scanf("%d",&a[i]);
memset(dp,INF,sizeof(dp));
for (int i = 0; i < n; i++)
*lower_bound(dp,dp+n,a[i]) = a[i];
printf("%d\n",lower_bound(dp,dp+n,INF)-dp);
}
Testing the CATCHER
Description
A
military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile called the CATCHER which is capable of intercepting multiple incoming offensive missiles. The CATCHER is supposed to be a remarkable
defensive missile. It can move forward, laterally, and downward at very fast speeds, and it can intercept an offensive missile without being damaged. But it does have one major flaw. Although it can be fired to reach any initial elevation, it has no power
to move higher than the last missile that it has intercepted.
The tests which the contractor completed were computer simulations of battlefield and hostile attack conditions. Since they were
only preliminary, the simulations tested only the CATCHER's vertical movement capability. In each simulation, the CATCHER was fired at a sequence of offensive missiles which were incoming at fixed time intervals. The only information available to the CATCHER
for each incoming missile was its height at the point it could be intercepted and where it appeared in the sequence of missiles. Each incoming missile for a test run is represented in the sequence only once.
The result of each test is reported as the sequence of incoming missiles and the total number of those missiles that are intercepted
by the CATCHER in that test.
The General Accounting Office wants to be sure that the simulation test results submitted by the military contractor are attainable,
given the constraints of the CATCHER. You must write a program that takes input data representing the pattern of incoming missiles for several different tests and outputs the maximum numbers of missiles that the CATCHER can intercept for those tests. For any
incoming missile in a test, the CATCHER is able to intercept it if and only if it satisfies one of these two conditions:
The incoming missile is the first missile to be intercepted in this test.
-or-
The missile was fired after the last missile that was intercepted and it is not higher than the last missile which was intercepted.
Input
The input data for any test consists of a sequence of one or more non-negative
integers, all of which are less than or equal to 32,767, representing the heights of the incoming missiles (the test pattern). The last number in each sequence is -1, which signifies the end of data for that particular test and is not considered to represent
a missile height. The end of data for the entire input is the number -1 as the first value in a test; it is not considered to be a separate test.
Output
Output
for each test consists of a test number (Test #1, Test #2, etc.) and the maximum number of incoming missiles that the CATCHER could possibly intercept for the test. That maximum number appears after an identifying message. There must be at least one blank
line between output for successive data sets.
Note: The number of missiles for any given test is not limited. If your solution is based on an inefficient algorithm, it may not
execute in the allotted time.
Sample
Input
389 207 155 300 299 170 158 65 -1 23 34 21 -1 -1Sample Output
Test #1: maximum possible interceptions: 6 Test #2: maximum possible interceptions: 2
题目叙述非常繁琐,大意就是输入任意个数字,求相应的最长下降子序列。
我用不同的数据测试了一下,每个测试样例的序列长度不会超过2000,所以用n²和nlogn的做法都可以完成。
贴一个用二分法做的版本。
#include <cstdio>
#include <cstring>
int c[2000],len=-1;
int find(int t)
{
int le=0,ri=len,mid;
while(le <= ri)
{
mid = (le+ri) >> 1;
if(c[mid] == t)
return mid;
if(c[mid] < t)
ri = mid - 1;
else le = mid + 1;
}
return le;
}
int main()
{
int t,n,m=0;
c[0] = 0;
while(scanf("%d",&t))
{
if(t == -1)
if(len == -1) return 0;
else
{
if(m++) printf("\n");
printf("Test #%d:\n maximum possible interceptions: %d\n",m,len+1);
c[0] = 0;
len = -1;
}
else
{
n = find(t);
c[n] = t;
if(n > len) len = n;
}
}
return 0;
}