ACM: 动态规划 poj 1887

来源:互联网 发布:linux ed2k 终端 编辑:程序博客网 时间:2024/06/14 14:40
                                                               Testing the CATCHER
Description
A militarycontractor for the Department of Defense has just completed aseries of preliminary tests for a new defensive missile called theCATCHER which is capable of intercepting multiple incomingoffensive missiles. The CATCHER is supposed to be a remarkabledefensive missile. It can move forward, laterally, and downward atvery fast speeds, and it can intercept an offensive missile withoutbeing damaged. But it does have one major flaw. Although it can befired to reach any initial elevation, it has no power to movehigher than the last missile that it has intercepted.

The tests which the contractor completed were computer simulationsof battlefield and hostile attack conditions. Since they were onlypreliminary, the simulations tested only the CATCHER's verticalmovement capability. In each simulation, the CATCHER was fired at asequence of offensive missiles which were incoming at fixed timeintervals. The only information available to the CATCHER for eachincoming missile was its height at the point it could beintercepted and where it appeared in the sequence of missiles. Eachincoming missile for a test run is represented in the sequence onlyonce.

The result of each test is reported as the sequence of incomingmissiles and the total number of those missiles that areintercepted by the CATCHER in that test.

The General Accounting Office wants to be sure that the simulationtest results submitted by the military contractor are attainable,given the constraints of the CATCHER. You must write a program thattakes input data representing the pattern of incoming missiles forseveral different tests and outputs the maximum numbers of missilesthat the CATCHER can intercept for those tests. For any incomingmissile in a test, the CATCHER is able to intercept it if and onlyif it satisfies one of these two conditions:

The incoming missile is the first missile to be intercepted in thistest.
-or-
The missile was fired after the last missile that was interceptedand it is not higher than the last missile which wasintercepted.

Input

The input data forany test consists of a sequence of one or more non-negativeintegers, all of which are less than or equal to 32,767,representing the heights of the incoming missiles (the testpattern). The last number in each sequence is -1, which signifiesthe end of data for that particular test and is not considered torepresent a missile height. The end of data for the entire input isthe number -1 as the first value in a test; it is not considered tobe a separate test.

Output

Output for each testconsists of a test number (Test #1, Test #2, etc.) and the maximumnumber of incoming missiles that the CATCHER could possiblyintercept for the test. That maximum number appears after anidentifying message. There must be at least one blank line betweenoutput for successive data sets.

Note: The number of missiles for any given test is not limited. Ifyour solution is based on an inefficient algorithm, it may notexecute in the allotted time.

Sample Input

38920715530029917015865-1
233421-1
-1

Sample Output

Test #1:  maximum possible interceptions: 6
Test #2: maximum possible interceptions: 2

题意: 求最长的降序.

解题思路:
         1. 状态: dp[i]: 在序列中前i个数字的最大降序长度.
         2. 状态转移方程:
                      当a[i] < a[j] 时, (0 <= j <= i)
                          dp[i] = max(dp[i],dp[j]+1);

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 10005

int n;
int a[MAX];
int dp[MAX];

inline int max(int a,int b)
{
    return a > b ? a : b;
}

int main()
{
    int caseNum = 1;
//    freopen("input.txt","r",stdin);
    n = 0;
    while(scanf("%d",&a[n]) != EOF)
    {
        if(a[n] == -1)
            break;
        dp[n++] = 1;
        int temp;
        while(true)
        {
            scanf("%d",&temp);
            if(temp == -1)
                break;
            else
            {
                a[n] = temp;
                dp[n++] = 1;
            }
        }
       
        int result = 0;
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j <= i; ++j)
            {
                if(a[i] < a[j])
                {
                    dp[i] = max(dp[i],dp[j]+1);
                }
            }
            result = max(result,dp[i]);
        }
        printf("Test #%d:\n  maximum possible interceptions: %d\n\n",caseNum++,result);
        n = 0;
    }
    return 0;
}
0 0