Partial Sequence of the Longest Increase

转载 2013年12月06日 08:15:04

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;
}



举报

相关文章推荐

【codeforces 466D】Increase Sequence

【题目链接】:http://codeforces.com/problemset/problem/466/D【题意】 给你n个数字; 让你选择若干个区间; 且这些区间[li,ri]; 左端点...

find the longest of the shortest

Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she do...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

【DP】 codeforces 466D Increase Sequence

很巧妙的DP。。。转化成每次都jian

HDU1595 find the longest of the shortest

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1595 find the longest of the shortest Time Li...

hdu1595-find the longest of the shortest

http://acm.hdu.edu.cn/showproblem.php?pid=1595 求最短路中间的最长路径 题意为求最短路,但是要求的是最短路中间当去掉某一条路时的情况下路径最长的,这里...

Longest Consecutive Sequence

3到4星级难度。 考点:活用hash表 利用?:操作可以简化让程序更加简洁。 注意: 1 数组中的数字会有重复,不要重复处理数据。 2 利用hash数据结构存储新增的数字,更新连续数字两边的边界值。 ...

the longest distance of a binary tree

the longest distance of a binary tree 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索“cqs_2012”即可; 个人爱好:酷...

HDU-1595-find the longest of the shortest

ACM模版描述 题解先dij一遍,求出0到n-1的最短路径,然后枚举每一段,求出删除该段时的最短路,最后求所有情况的最短路径的最大花费。多次dij搞搞。这里需要记住的是,对dij要进行一个改造,不能...
  • f_zyj
  • f_zyj
  • 2016-11-25 17:54
  • 120

HDU 1595 find the longest of the shortest

记忆搜索

HDU 1595 find the longest of the shortest

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 题意是说任意去掉一条边之后的...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)