What is a bigger smarter? (LIS, print path)

Source: Internet
Author: User

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



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.