What Is the biggest Smarter? (Dynamic Planning)

Source: Internet
Author: User

Is Bigger Smarter?
The Problem
Some people think that the bigger an elephant is, the smarter it is. to disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. the data for a participating elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. both integers are between 1 and 10000. the data will contain in information for at most 1000 elephants. two elephants may have the same weight, the same IQ, or even the same weight and IQ.

Say that the numbers on the I-th data line are W [I] and S [I]. your program shocould output a sequence of lines of data; the first line shocould contain a number n; the remaining n lines shocould each contain a single positive integer (each one representing an elephant ). if these n integers are a [1], a [2],..., a [n] then it must be the case that

W [a [1] <W [a [2] <... <W [a [n]
And
S [a [1]> S [a [2]>...> S [a [n]
In order for the answer to be correct, n shoshould be as large as possible. all inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. there may be required correct outputs for a given input, your program only needs to find one.

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. Question Analysis: It is required to find out the largest ascending sequence of weight and IQ in ascending order. Solution: 1. Sort elephants by weight from small to large. 2. traverse all elephants from the front to the back, and open a dynamic rolling array dp [I] to represent the sequence length formed by the current elephant for the end, dp [j] indicates the sequence length that ends with an elephant that meets the requirements before I. If dp [j] + 1> dp [I], then dp [I] = dp [j] + 1, use path [I] to write down j (the elephant before I ). 3. output the length of the longest sequence and recursively output the path.

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; struct ele {int w, s, id;} a [1005]; int dp [1005], path [1005], MAX =-1; bool comp (ele a1, ele a2) {return a1.w <a2.w;}/* recursive output path */void printpath (int I) {if (MAX --) {printpath (path [I]); printf ("% d \ n", a [I]. id) ;}} int main () {int I, j, p, k; k = 1; while (~ Scanf ("% d", & a [k]. w, & a [k]. s) {a [k]. id = k; k ++;} k --; sort (a + 1, a + k, comp); for (I = 1; I <= k; I ++) {dp [I] = 1; path [I] = I ;}for (I = 1; I <= k; I ++) {for (j = 1; j <I; j ++) {if (a [I]. w> a [j]. w & a [I]. s <a [j]. s & dp [I] <dp [j] + 1) {dp [I] = dp [j] + 1; path [I] = j; /* record the elephant before I */} if (dp [I]> MAX) {MAX = dp [I]; p = I ;}}} printf ("% d \ n", MAX); printpath (p); return 0 ;}

 

Related Article

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.