// Is bigger smarter? (What is the bigger the smarter ?) // PC/ultraviolet A IDs: 111101/10131, popularity: B, success rate: high level: 2 // verdict: accepted // submission date: 2011-10-10 // UV Run Time: 0.032 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [Problem description] // some people think that the larger the elephant is, the smarter the brain is. To refute this incorrect idea, you want to analyze the data of a group of elephants and find out a sequence of elephants with a strict increasing weight but a strict decreasing IQ. //// [Enter] // enter the data of several elephants. Each row contains one elephant until the input ends. The data for each elephant consists of two integers: the first is the weight in kilograms, and the second is the IQ index in the whole hundred units. The two integers are between 1 and 10000. The input can contain a maximum of 1000 heads/elephant. Two elephants may have the same weight, the same IQ, or even the same weight and IQ. //// [Output] // The first line of output should contain an integer N, which is the length of the found elephant sequence. In the next n rows, each line contains a positive integer, indicating an elephant. Use W [I] And s [I] to represent the two numbers of row I in the input data. If this sequence is found as a [1], a [2], //..., A [n], you must have: // W [A [1] <W [A [2] <... <W [A [n] and S [A [1]> S [A [2]>...> s [A [n] I /// here, N should be the maximum possible value. All unequal relations are strictly unequal: weight increases strictly, while IQ decreases strictly. // If multiple groups of solutions exist, your program only needs to output one solution. /// [Sample input] // 6008 1300 6000 // 2100 500 // 2000 1000 // 4000 1100 // 3000 6000 // 2000 8000 // 1400 6000 /// 1200 // 2000 1900 // [sample output] // 4 // 4 // 5 // 9 // 7 // [solution] // sort the elephants in ascending order of weight, if the two elephants share the same weight, they are sorted in descending order of IQ. After sorting, the problem is converted to a longest descending subsequence (LDS) of IQ //. During calculation, there are additional conditions, that is, the body // weight should be incremental, then you can use an array length [] to record the LDs length that can be obtained by the I header, when processing the data of the first/I + 1 elephant, I used to find an elephant whose weight was less than I + 1 but whose IQ was higher than l The longest Number of the elephants in DS. Update the LDs length of the I + 1 elephant and record the LDs that the I + 1 elephant received after the elephant, this can be recorded by setting up a parnet [] array. # Include <iostream> # include <algorithm> using namespace STD; # define maxn 1000 class elephant {public: int index; int weight; int IQ; bool operator <(const elephant & Other) const {If (weight! = Other. weight) return weight <Other. weight; return IQ> Other. IQ ;}}; elephant elephants [maxn]; int length [maxn]; // records the LDs length that an elephant can obtain. Int parent [maxn]; // select a record. // Use recursion to output the answer in reverse order. Void backtrack (INT index) {If (parent [Index]! =-1) backtrack (parent [Index]); cout <(ELEPHANTS [Index]. index + 1) <Endl;} int main (int ac, char * AV []) {int Index = 0, weight, IQ; while (CIN> weight> IQ) elephants [index ++] = (ELEPHANT) {index, weight, IQ}; // sort by weight in ascending order, find the longest descending sequence of IQ, or you can sort by IQ in descending order to find the longest ascending sequence of weight. Sort (elephants, elephants + index); // The initial lDs length is set to 1. For (INT I = 0; I <index; I ++) {length [I] = 1; parent [I] =-1 ;}// DP evaluate the LDs length. Int maxlength = 0, maxindex = 0; For (INT I = 0; I <index; I ++) {int IQ = elephants [I]. IQ; int Weight = elephants [I]. weight; For (Int J = 0; j <I; j ++) {If (ELEPHANTS [J]. weight <weight & elephants [J]. IQ> IQ) if (length [I] <= length [J]) {length [I] = length [J] + 1; parent [I] = J ;}} if (maxlength <length [I]) {maxlength = length [I]; maxindex = I ;}// uses recursive inversion to output results. Cout <maxlength <Endl; backtrack (maxindex); Return 0 ;}