Va 103-stacking boxes (LIS, print path)

Source: Internet
Author: User

Link: Ultraviolet A 103

Question: For n-dimensional images, their edge lengths are {D1, D2, D3.. DN}. For two n-dimensional images, find all the edge lengths that satisfy one of them.

FollowAny orderBothOne-to-one correspondenceSmaller than the length of the other side, the number of such longest sequence, and print the path of any of the longest string,

For example: A (,), B (,), C (,), A and B do not meet, but C and B meet

Analysis: First, sort the no-group edge length from small to large, then sort the images of each group by the smallest edge, and then find the maximum substring,

Print paths can be iterated in reverse or recursive order.

# Include <cstdio> # include <algorithm> using namespace STD; int DP [35], path [35], num, M, K; struct Stu {int A [12], ID;} s [35]; int CMP (struct Stu S1, struct Stu S2) {return s1.a [1] <s2.a [1];}/* void back_path1 (int I) {If (path [I]! = I) back_path1 (path [I]); printf ("% d", s [I]. ID); num ++; If (num! = M) printf (""); else printf ("\ n");} * // * void back_path2 (int I) {If (k --) {back_path2 (path [I]); printf ("% d", s [I]. ID); num ++; If (num! = M) printf (""); else printf ("\ n") ;}} */INT main () {int I, j, N, POs, B [1005]; while (scanf ("% d", & M, & N )! = EOF) {for (I = 1; I <= m; I ++) {s [I]. id = I; for (j = 1; j <= N; j ++) scanf ("% d", & S [I]. A [J]); sort (s [I]. A + 1, s [I]. A + n + 1); // sort the edge length of each graph} Sort (S + 1, S + m + 1, CMP); // between each graph, sort by minimum edge length (I = 1; I <= m; I ++) {DP [I] = 1; path [I] = I; for (j = 1; j <I; j ++) {for (k = 1; k <= N; k ++) if (s [J]. A [k]> = s [I]. A [k]) break; If (k = n + 1 & DP [J] + 1> DP [I]) {DP [I] = DP [J] + 1; path [I] = J ;}} Pos = 1; for (I = 2; I <= m; I ++) if (DP [I]> DP [POS]) Pos = I; M = DP [POS]; printf ("% d \ n", M ); B [1] = s [POS]. ID; // Add the last number to I = 2; for (j = pos-1; j> = 1; j --) {// returns the path for (k = 1; k <= N; k ++) if (s [J]. A [k]> = s [POS]. A [k]) break; If (k = n + 1 & DP [J] + 1 = DP [POS]) {B [I ++] = s [J]. ID; DP [POS] --;} If (DP [POS] = 1) break;} For (j = I-1; j> 1; j --) printf ("% d", B [J]); printf ("% d \ n", B [1]);/* num = 0; // recursive method 1 back_path1 (POS); * // * num = 0; // recursive method 2 k = m; back_path2 (POS); */} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.