zoukankan      html  css  js  c++  java
  • LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法

    正好训练赛来了一道最长递减序列问题,所以好好研究了一下最长递增序列问题。

    B - Testing the CATCHER
    Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

    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
    
     

    一种是动态规划方法

    #include <iostream>
    #include <cstdio>
    int f[10000];
    int p[10000];
    int main()
    {
        int count=0;
        int cur=0;
        while (scanf("%d",&p[cur++]))
        {
            if (p[cur-1]==-1)
             if (cur-1==0) break;
             else
             {
                 int i,j,k;
                 for (i=0;i<cur-1;i++)
                  f[i]=1;
                 for (i=0;i<cur-1;i++)//类似于背包一样进行物品循环
                 {
                     for (j=i-1;j>=0;j--)//类似于循环背包容量。
                     {
                         if (p[i]<=p[j]) //此题求得是递减序列,故符号位<=
                          if (f[i]<f[j]+1) f[i]=f[j]+1; 
                     }
                 }
                 int ans=0;
                 for (i=0;i<cur-1;i++)
                 {
                     if (ans<f[i]) ans=f[i];//此时的最大量不一定是坐标最大时,所以要这样来一下。
                 }
                 printf("Test #%d:
    ",++count);
                 printf("  maximum possible interceptions: %d
    
    ",ans);
                 cur=0;
             }
    
        }
        return 0;
    }

    还有一种方法是贪心加二分的方法。

    贴了一下大神博客的内容

    开一个栈,每次取栈顶元素top和读到的元素temp做比较,如果temp > top 则将temp入栈;如果temp < top则二分查找栈中的比temp大的第1个数,并用temp替换它。 最长序列长度即为栈的大小top。

    这也是很好理解的,对于x和y,如果x < y且Stack[y] < Stack[x],用Stack[x]替换Stack[y],此时的最长序列长度没有改变但序列Q的''潜力''增大了。

    举例:原序列为1,5,8,3,6,7

    栈为1,5,8,此时读到3,用3替换5,得到1,3,8; 再读6,用6替换8,得到1,3,6;再读7,得到最终栈为1,3,6,7。最长递增子序列为长度4。

    用该算法完成POJ2533的具体代码如下:

     
    #include <iostream>
    #define SIZE 1001
     
    using namespace std;
     
    int main()
    {
        int i, j, n, top, temp;
        int stack[SIZE];
        cin >> n;
     
        top = 0;
        /* 第一个元素可能为0 */
        stack[0] = -1;
        for (i = 0; i < n; i++)
        {
            cin >> temp;
            /* 比栈顶元素大数就入栈 */
            if (temp > stack[top])
            {
                stack[++top] = temp;
            }
            else
            {
                int low = 1, high = top;
                int mid;
                /* 二分检索栈中比temp大的第一个数 */
                while(low <= high)
                {
                    mid = (low + high) / 2;
                    if (temp > stack[mid])
                    {
                        low = mid + 1;
                    }
                    else
                    {
                        high = mid - 1;
                    }
                }
                /* 用temp替换 */
                stack[low] = temp;
            }
        }
     
        /* 最长序列数就是栈的大小 */
        cout << top << endl;
     
        //system("pause");
        return 0;
    }

    关于这道题,我的代码是

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int stack[10000];
    int p[10000];
    
    int main()
    {
        int count=0;
        int cur=0;
        while (scanf("%d",&p[cur++]))
        {
            if (p[cur-1]==-1)
                if (cur-1==0) break;
                else
                {
                    int i,j,k;
                    int top=0;
                    stack[top++]=p[0];//模拟棧的操作。
                    for (i=1; i<cur-1; i++)
                    {
                        if (p[i]<stack[top-1])
                            stack[top++]=p[i];
                        else
                        {
                            int l=0,r=top,mid;
                            while (l<r)//二分的地方要注意,在递增序列里,要使得能搜到正好大于p[i]的那个点。。。若为递减序列,则要能正好搜到小于p[i]的那个点
                            {
                                mid=(l+r)/2;
                                if (stack[mid]<p[i]) r=mid;//因为是递减序列,所以为小于
                                else
                                    l=mid+1;
                            }
                            stack[l]=p[i];
                        }
                    }
                    printf("Test #%d:
    ",++count);
                    printf("  maximum possible interceptions: %d
    
    ",top);
                    cur=0;
                }
    
        }
        return 0;
    }
  • 相关阅读:
    luogu_1659【题解】manacher 啦啦队排练
    manacher算法
    luogu_4503【题解】企鹅QQ 哈希
    luogu_3966【题解】单词 AC自动机
    字符串 AC自动机
    luogu_3275【题解】糖果 差分约束
    luogu_4568 飞行路线 分层图
    luogu_4551【题解】最长异或路径 trie树
    luogu_1041【题解】搜索 传染病控制
    [题解/模板]扫描线
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3234639.html
Copyright © 2011-2022 走看看