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



LeetCode题解整理版(一)

Evaluate Reverse Polish Notation 逆波兰表达式求值,经典问题。 遇到数字入栈,遇到符号取栈顶的两个出来运算,再将结果入栈,最后栈里剩下的一个元素就是结果了。 ...
  • xualaleilei
  • xualaleilei
  • 2016年04月04日 17:30
  • 443

[Python] partial改变方法默认参数

Python 标准库中 functools 中有许多对方法hen
  • lzz957748332
  • lzz957748332
  • 2014年08月18日 10:33
  • 1921

最长公共子序列(Longest Common Sequence)

问题的定义: 子序列 –X=(A, B, C, B, D, B) – Z=(B, C, D, B)是X的子序例 –W=(B, D, A)不是X的子序例 公共子序列 – Z是序列X与Y的公共子序...
  • u013068755
  • u013068755
  • 2017年04月22日 16:22
  • 605

最长递增和递减子序列详解(longest increasing subsequence)

对于动态规划问题,往往存在递推解决方法,这个问题也不例外。要求长度为i的序列的Ai{a1,a2,……,ai}最长递增子序列,需要先求出序列Ai-1{a1,a2,……,ai-1}中以各元素(a1,a2,...
  • seekcreation
  • seekcreation
  • 2014年10月09日 21:45
  • 674

C# 窗口类定义中的 partial 关键字详解

我们新建一个Windows Form时,后台代码都会自动添加如下代码: 1 public partial class Form1 : Form 2 { 3     public Form1() ...
  • wonengguwozai
  • wonengguwozai
  • 2016年09月09日 15:37
  • 1171

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respe...
  • wangxiaojun911
  • wangxiaojun911
  • 2014年02月06日 03:32
  • 6382

C#关键字之Partial详解

Partial是局部类型的标志。局部类型可以实现将一个类、结构或接口分成几个部分,分别放在在几个不同的.cs文件中(当然也可以放在同一个.cs文件中)。在程序进行编译之后,将会合并成一个完整的类。因此...
  • yiyelanxin
  • yiyelanxin
  • 2017年04月25日 08:52
  • 945

【.Net码农】C#中的partial class(部分类)

C# 2.0 可以将类、结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可。 例如:下面的PartialTest类 class PartialTest ...
  • CrackLibby
  • CrackLibby
  • 2016年07月26日 16:26
  • 901

动态规划之最长递增子序列(Longest Increasing Subsequence)

原文地址:http://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/ We have...
  • sinat_36246371
  • sinat_36246371
  • 2016年10月13日 12:12
  • 939

部分和(partial sum)在算法求解中的作用

1. 部分和的引入 并非什么高级深奥的技巧,但却十分有用。 假设按照降序排列 NN 个学生的考试成绩并保存到数组 scores[],现在想要编写求出从第 a 名到第 b 名成绩的函数 averag...
  • lanchunhui
  • lanchunhui
  • 2016年09月09日 17:31
  • 1435
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Partial Sequence of the Longest Increase
举报原因:
原因补充:

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