Uvater is bigger smarter

Source: Internet
Author: User

Question:

The subsequence that calculates the longest weight increase and IQ decrease

Analysis:

It is actually the deformation of the longest ascending sub-sequence. In fact, it is very easy to sort by weight from small to large,

Then it's about Lis. to print the path, use the array path [] to record the location next to it, and use

Implement print recursively (For details, referAlgorithmIntroduction)

 

# Include <iostream>

# Include <cstdio>

# Include <cstring>

# Include <algorithm>

Using namespace STD;

# Define x 1005

Int DP [X], path [x];

Struct Node

{

Int IQ, W, ID; // record IQ, weight, and position before unsorted

} P [x];

Int CMP (struct node A, struct Node B) // sort by weight in non-descending order

{

Return A. W <B. W;

}

Void print (int id) // recursively print the path

{

Cout <p [ID]. ID <Endl; // because I use an array to record the position behind it, print the previous ID first

If (path [ID]! =-1)

Print (path [ID]); // recursively print the position following

}

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

Int CNT = 0;

While (scanf ("% d", & P [CNT]. W, & P [CNT]. IQ )! = EOF)

{

DP [CNT] = 1; // initialize DP

P [CNT]. ID = CNT + 1; // Record ID

CNT ++;

}

Sort (p, p + CNT, CMP); // sort the struct

Int ans = 0;

Memset (path,-1, sizeof (PATH ));

For (INT I = cnt-1; I> = 0; I --)

For (Int J = I + 1; j <CNT; j ++)

If (P [I]. IQ> P [J]. IQ & P [I]. W <p [J]. w) // if the weight is smaller than the following and the IQ is greater than the following

If (DP [I] <DP [J] + 1)

{

Path [I] = J; // record path

DP [I] = DP [J] + 1;

}

Int ID;

For (INT I = 0; I <CNT; I ++)

If (ANS <DP [I])

{

Ans = DP [I];

Id = I; // find the earliest ID

}

Cout <ans <Endl;

Print (ID );

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.