Partial Sequence of the Longest Increase

Partial Sequence of the Longest Increase

A problem to find a partial sequence of the longest increase is well-known in the algorism field.
This sequence is defined as the longest sequence among the partial sequence which is L_a ≤ L_b ≤ ... (a < b < ...) for the sequence L.
For instance, the maximum partial increase sequence from {1, 4, 5, 3, 2, 4, 5, 6} is {1, 2, 4, 5, 6} and {1, 3, 4, 5, 6}.


At this time, define the maximum partial increase sequence about two dimensional matrix T of N×N.
When choosing random elements for monotone increasing in the direction of increase the numbers of rows and columns,
this is called as the partial increase sequence of the matrix T.
In addition the longest one among the partial increase sequence of T is called as the maximum partial increase sequence of the matrix T.


Now, let’s find the length of the maximum increase partial sequence of the given sequence.


Time limit : 2 sec (Java : 4 sec)


[Input]


There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 )


In each test case,
In the first line, the number of the size of the matrix (N) is given. (1 ≤ N ≤ 256)
Information of the matrix is separated by blanks and given from the next line to the lines of the number N.


[Output]


In the each line, generate the length of the maximum increase partial sequence of the given matrix.


[Input Example]


2
4
3 2 4 6
2 1 1 8
1 5 2 4
7 6 7 4
5
9 9 1 6 4
2 1 8 1 6
4 8 6 8 3
8 9 4 6 3
6 4 9 4 9


[Output Example]


3
4


[Explanation of an example]


The increase partial sequence of the length 4 is not existed.
The increase partial sequence of the length 3 is five: {3, 5, 7}, {2, 2, 4}, {2, 5, 7}, {2, 2, 4}, {1, 2, 4}
In case of {2, 2, 4}, there are two cases starting from (1, 2) of the matrix and starting from (2, 1) of the matrix.
The increase partial sequence of the length 3 has the longest length.
Because of that, the length of the maximum increase partial sequence is three.


solution

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

#define MAX_DIM  256

static unsigned short matrix[ MAX_DIM ][ MAX_DIM ];
static unsigned short references[ MAX_DIM * MAX_DIM ];
static int raw_digits[ MAX_DIM * MAX_DIM ];

static unsigned short *paths[ MAX_DIM  * 2 ];
static unsigned int counts[ MAX_DIM * 2 ];


int cmp_refs( const void *a, const void *b )
{
    return ( raw_digits[ *(unsigned short*)a ] - raw_digits[ *(unsigned short*)b ] );
}


void data_set_add_number( unsigned short *paths_ptr, unsigned int *count_ptr, unsigned short number )
{
    int i;

    for( i = *count_ptr; i <= number; ++i )
        paths_ptr[i] = paths_ptr[i - 1];

    if( *count_ptr <= number )
        *count_ptr = number + 1;

    paths_ptr[ number ]++;
    for( i = number + 1; i < *count_ptr && paths_ptr[i] < paths_ptr[i - 1]; ++i )
        paths_ptr[i] = paths_ptr[i - 1];
}


void copy_data_set( unsigned short *paths1, unsigned int *count1,
                    const unsigned short *paths2, unsigned int count2 )
{
    *count1 = count2;
    if( count2 != 0 )
        memcpy( paths1, paths2, count2 * sizeof(unsigned short) );
}


void merge_data_sets( unsigned short *paths1, unsigned int *count1,
                      const unsigned short *paths2, unsigned int count2 )
{
    int i;
    int max_merge = (*count1 > count2) ? count2 : *count1;

    for( i = 0; i < max_merge; ++i )
        if( paths1[i] < paths2[i] )
            paths1[i] = paths2[i];

    if( *count1 < count2 )
    {
        memcpy( paths1 + max_merge, paths2 + max_merge, (count2 - max_merge) * sizeof(unsigned short) );
        *count1 = count2;
    }

    for( i = max_merge; i < *count1 && paths1[i] < paths1[i - 1]; ++i )
        paths1[i] = paths1[i - 1];
}


int main()
{
    int T, N;
    int t, i, j, k, maxDigit;

    unsigned short **cur_paths, **prev_paths, **tmp_paths;
    unsigned int *cur_counts, *prev_counts, *tmp_counts;

    paths[0] = malloc( 2 * MAX_DIM * MAX_DIM * MAX_DIM * sizeof(unsigned short) );
    for( i = 1; i < MAX_DIM * 2; ++i )
        paths[i] = paths[i-1] + MAX_DIM * MAX_DIM;

    cur_paths = paths;
    prev_paths = cur_paths + MAX_DIM;

    cur_counts = counts;
    prev_counts = cur_counts + MAX_DIM;

    scanf( "%d", &T );
    for( t = 0; t < T; ++t )
    {
        scanf( "%d", &N );

        k = 0;
        for( i = 0; i < N; ++i )
            for( j = 0; j < N; ++j )
            {
                matrix[i][j] = k;
                references[k] = k;
                scanf( "%d", &raw_digits[k++] );
            }

        qsort( references, k, sizeof(unsigned short), cmp_refs );

        j = maxDigit = 0;
        for( i = 1; i < k; ++i )
        {
            if( raw_digits[ references[i] ] != raw_digits[ references[i-1] ] )
                maxDigit++;

            raw_digits[ references[i-1] ] = j;
            j = maxDigit;
        }
        raw_digits[ references[k-1] ] = j;

        for( i = 0; i < N; ++i )
            for( j = 0; j < N; ++j )
                matrix[i][j] = raw_digits[ matrix[i][j] ];

        for( i = 0; i < N; ++i )
        {
            cur_paths[i][0] = 0;
            cur_counts[i] = 1;
        }

        for( i = 0; i < N; ++i )
        {
            tmp_paths = cur_paths;
            cur_paths = prev_paths;
            prev_paths = tmp_paths;

            tmp_counts = cur_counts;
            cur_counts = prev_counts;
            prev_counts = tmp_counts;

            cur_paths[0][0] = 0;
            cur_counts[0] = 1;
            data_set_add_number( cur_paths[0], &cur_counts[0], matrix[i][0] );

            for( j = 1; j < N; ++j )
            {
                copy_data_set( cur_paths[j], &cur_counts[j], prev_paths[j - 1], prev_counts[j - 1] );
                data_set_add_number( cur_paths[j], &cur_counts[j], matrix[i][j] );

                merge_data_sets( cur_paths[j], &cur_counts[j], cur_paths[j - 1], cur_counts[j - 1] );
            }

            if( i != 0 )
                for( j = 0; j < N; ++j )
                    merge_data_sets( cur_paths[j], &cur_counts[j], prev_paths[j], prev_counts[j] );
        }

        printf( "%d\n", (int) cur_paths[N-1][ cur_counts[N-1] - 1 ] );
    }

    free( paths[0] );
    return 0;
}



阅读更多
个人分类: Algorithm
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Partial Sequence of the Longest Increase

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭