Partial Sequence of the Longest Increase
来源:互联网 发布:王阳明 道家 知乎 编辑:程序博客网 时间:2024/06/04 19:13
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 256static 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;}
- Partial Sequence of the Longest Increase
- Increase Sequence
- CareerCup the maximum longest continous sequence of 0s
- Finding the Longest Nondecreasing Subsequence of A Given Sequence:An analysis of dynamic programming
- Increase the ordinal number at the beginning of each line
- 【CODEFORCES】 D. Increase Sequence
- jQuery:mouseover and Increase the Size of an Image
- jQuery:mouseover and Increase the Size of an Image
- Codeforces 466 D. Increase Sequence
- hdu1595-find the longest of the shortest
- HDU1595---find the longest of the shortest
- hdu1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- find the longest of the shortest
- find the longest of the shortest HDU
- find the longest of the shortest HDU1595
- find the longest of the shortest HDU
- ENGLISH资料收集(41)-怎么应对电话推销?
- HLA高级汇编 进入高级汇编语言的世界 第一篇
- 如何把本地MSSQL数据库发布到远程服务器上_视频在线观看
- Unity 网络部分
- Apache中如何使用ErrorDocument标签设定统一的错误页面
- Partial Sequence of the Longest Increase
- C#用UDP异步发送信息
- C#用UDP同步发送数据和接受数据
- C#利用广播实现群发功能
- C#利用组播实现网络会议室功能(即群聊功能)
- 基于信仰之下的努力——Leo鉴书38
- Treeset和HashSet有什么不同(复习)
- RHEL 6.4(i386)编译安装git 1.8.5
- C#对字符串加密