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