POJ 1887 Testing the CATCHER(LIS的反面 最大递减子序列)


Language:
Default

Testing the CATCHER

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15207   Accepted: 5595

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
-1

Sample Output

Test #1:
  maximum possible interceptions: 6

Test #2:
  maximum possible interceptions: 2

Source

World Finals 1994

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 10005

int a[N],dp[N];

int main()
{
	int i,j,n,x,ca=0;
	while(scanf("%d",&x))
	{
		if(x==-1) break;
		n=0;
		a[n++]=x;
		while(1)
		{
			scanf("%d",&x);
			if(x==-1) break;
			a[n++]=x;
		}

		dp[0]=1;
		int ans=1;
		int temp;
		for(i=1;i<n;i++)
		{
			temp=0;
			for(j=0;j<i;j++)
				if(a[j]>a[i]&&temp<dp[j])
				temp=dp[j];

			dp[i]=temp+1;
			if(ans<dp[i])
				ans=dp[i];
		}
	  printf("Test #%d:\n",++ca);
      printf("  maximum possible interceptions: %d\n\n",ans);
	}
     return  0;
}
时间: 2024-08-27 06:18:17

POJ 1887 Testing the CATCHER(LIS的反面 最大递减子序列)的相关文章

POJ 1887 Testing the CATCHER.

~~~~ 求最长不上升子序列,把数组倒过来不就是求最长上升子序列了么,QAQ.. 用的是nlogn算法,不清楚的请戳:http://blog.csdn.net/darwin_/article/details/38360997 题目链接:http://poj.org/problem?id=1887 ~~~~ #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #de

[2016-03-11][POJ][1887][Testing the CATCHER]

Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u Submit Status Description A military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile cal

poj 1952 BUY LOW, BUY LOWER (最长递减子序列+不同子序列计数)

BUY LOW, BUY LOWER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8327   Accepted: 2888 Description The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also f

POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) 的数字序列, 要你求该序列中的最长(严格)下降子序列的长度. 分析:        读取全部输入, 将原始数组逆向, 然后求最长严格上升子序列就可以. 因为n的规模达到20W, 所以仅仅能用O(nlogn)的算法求.        令g[i]==x表示当前遍历到的长度为i的全部最长上升子序列中的最小序列末

POJ1887——Testing the CATCHER

Testing the CATCHER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15246   Accepted: 5612 Description A military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile calle

尝试解释LIS(最长递增子序列)的一种动态规划算法

最长上升子序列就是求给定序列的最长的递增序列,其中不要求序列的元素在原序列中保持连续. 为了方便理解,可以举个例子: inta[] = {0,2,1,5,3,6,4,8,9,7}(数组下标从1开始)的一个最长的子序列1,3,4,7,9. 利用动态规划的思想,可以方便的求取这个解. 为了方便解释,我们定义dp(n)为长度为1至下标为n的最长子序列的长度(数组下标假设从1开始),{a[1],a[2],..,a[n]}为dp(n)对应的序列. 为了和程序对应,我采取自底向上的方式进行解释. 1.显然对

poj 2533 &amp; poj 1631 Longest Ordered Subsequence( LIS果题 )

题目链接: POJ 2533:http://poj.org/problem?id=2533 POJ 1631:http://poj.org/problem?id=1631 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1,

POJ 2533 Longest Ordered Subsequence(LIS:最长上升子序列)

http://poj.org/problem?id=2533 题意: 给你一个长度为n的数字序列, 要你求该序列中的最长(严格)上升子序列的长度. 分析: 解法一: O(n^2)复杂度. 令dp[i]==x 表示以第i个数字结尾的上升子序列中最长的为x长度. 初始化: dp[0]=0且dp[i]=1 i>=1时. 状态转移: dp[i] =max( dp[j]+1 ) 其中j<i 且a[j]<a[i]. 最终所求:max(dp[i])  其中1<=i<=n. 解法二: O(n

poj 1836 Alignment(dp,LIS)

链接:poj 1836 题意:士兵站成一行,求最少要多少的士兵出列, 使得每个士兵都能至少看到一个最边上的士兵 中间某个人能看到最边上的士兵的条件是: 该士兵的身高一定强大于他某一边(左边或右边)所有人的身高, 身高序列可以是: 1  2  3   4   5   4   3   2   1  或者 1   2   3   4   5   5   4   3   2   1 分析:要求最少出列数,就是留队士兵人数最大, 即左边的递增序列人数和右边的递减序列人数之和最大 因而可转化为求"最长升序子