Blue Bridge Cup training algorithm ALGO-13 interceptor missiles

Training algorithm interceptor missile  

Time limit: 1.0s Memory Limit: 256.0MB

 

Description of the problem
  in a country in order to defend against enemy missile attacks, to develop a missile interception system. But such a missile interceptor system has a flaw: Although it's the first rounds to reach any height, but each shell and should not be higher than before the situation got height. One day, the radar picked up incoming enemy missiles. As the system is still in beta, so only a system, and therefore may not intercept all the missiles.

  Enter the missile turn flying height (height of the radar data given is not a positive integer greater than 30,000), calculate how much the system up to intercept the missile, if you want to intercept all missiles minimum number of sets to be equipped with such a missile interception system.

 

Enter the format
  line, in order for the missile flying height

 

Output format of
  two lines, respectively, up to the number of interceptor missiles to intercept and number of all missiles with a minimum of system

 

Sample input
389,207,155,300,299 170 158 65

 

Sample output
. 6
2
 

#include <stdio.h>
#include <stdlib.h>

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

int longest_nonstrictly_decreasing_subsequence(int arr[], int n)
{
    int *f = (int *)malloc(n * sizeof(int));

    f[0] = 1;
    for (int i = 1; i < n; ++i)
    {
        f[i] = 1;
        for (int k = 0; k < i; ++k)
        {
            if (arr[k] >= arr[i])
                f[i] = max(f[i], f[k] + 1);
        }
    }

    int max_len = 0;
    for (int i = 0; i < n; ++i)
        max_len = max(max_len, f[i]);
    return max_len;
}

int longest_strictly_increasing_subsequence(int arr[], int n)
{
    int *f = (int *)malloc(n * sizeof(int));

    f[0] = 1;
    for (int i = 1; i < n; ++i)
    {
        f[i] = 1;
        for (int k = 0; k < i; ++k)
        {
            if (arr[k] < arr[i])
                f[i] = max(f[i], f[k] + 1);
        }
    }

    int max_len = 0;
    for (int i = 0; i < n; ++i)
        max_len = max(max_len, f[i]);
    return max_len;
}

int main()
{
    int heights[10005] = { 0 };
    int n = 0;

    for (n = 0; scanf("%d", &heights[n]) != EOF; ++n)
        ;

    printf("%d\n", longest_nonstrictly_decreasing_subsequence(heights, n));
    printf("%d", longest_strictly_increasing_subsequence(heights, n));

    return 0;
}

 

Published 221 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/liulizhi1996/article/details/104025524