Link: Ultraviolet A 10131
Question: Given the weights and IQ values of several elephants, find the maximum number of sequences that meet the needs of the elephants to strictly increase their weights and strictly decrease their IQ,
Print the number of any group of elephants that have obtained the maximum value.
Analysis: This is the application of LIS, but there are two conditions for judging. You can sort the weights of elephants first, but print the path,
The path must be traced back and can be directly obtained in reverse order. recursion is also a good choice.
# Include <cstdio> # include <algorithm> using namespace STD; struct Stu {int size, IQ, ID;} A [1005]; int DP [1005], path [1005], M; int CMP (struct Stu a, struct Stu B) {if (. size! = B. Size) return a. Size <B. size; return a. IQ> B. IQ;} void back_path1 (int I) {If (path [I]! = I) back_path1 (path [I]); printf ("% d \ n", a [I]. ID);} void back_path2 (int I) {If (M --) {back_path2 (path [I]); printf ("% d \ n", a [I]. ID) ;}} int main () {int I = 1, J, N, K, B [1005]; while (scanf ("% d ", & A [I]. size, & A [I]. IQ )! = EOF) {A [I]. id = I; I ++;} n = I-1; sort (a + 1, A + n + 1, CMP); for (I = 1; I <= N; I ++) {DP [I] = 1; path [I] = I; for (j = 1; j <I; j ++) if (a [J]. size <A [I]. size & A [J]. IQ> A [I]. IQ & DP [J] + 1> DP [I]) {DP [I] = DP [J] + 1; path [I] = J ;}} k = 1; for (I = 2; I <= N; I ++) if (DP [I]> DP [k]) k = I; M = DP [k]; printf ("% d \ n", m); B [1] = A [K]. ID; I = 2; for (j = K; j> = 1; j --) // directly returns the path if (a [J] in reverse order. size <A [K]. size & A [J]. IQ> A [K]. IQ & DP [k] = DP [J] + 1) {B [I ++] = A [J]. ID; DP [k] --;} For (j = I-1; j> = 1; j --) printf ("% d \ n", B [J]); // back_path1 (k); // you can use two types of recursion to find the path // back_path2 (k); Return 0 ;}