Fatmouse's speed

Source: Internet
Author: User
Problem descriptionfatmouse believes that the fatter a mouse is, the faster it runs. to disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Inputinput contains data for a bunch of mice, one mouse per line, terminated by end of file. the data for a participating mouse will consist of a pair of integers: The first representing its size in grams and the second representing its speed in centimeters per second. both integers are between 1 and 10000. the data in each test case will contain information for at most 1000 mice. two mice may have the same weight, the same speed, or even the same weight and speed.
Outputyour 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 a mouse ). if these N integers are m [1], M [2],..., M [N] Then it must be the case that W [M [1] <W [M [2] <... <W [M [N] and S [M [1]> S [M [2]>...> s [M [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 speeds must be strictly decreasing. there may be required correct outputs for a given input, your program only needs to find one.
Sample input6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample output4
4
5
9
7
 
The requirement of this question is to output the longest arrangement. The first data in each group can be sorted from small to large, and the second data can be sorted from large to small, then, output the longest permutation number and the position of the current permutation number at the start.
I did not dare to submit the correct answers to all the evil questions. I am still making an error checking my code. After reading the senior code, I realized that my answer was correct.
# Include <cstdio>
# Include <algorithm>
Using namespace STD;
Struct mouse
{
Int W, S;
Int no;
} M [1005];
// Declare a mouse Array
Int f [1005]; // F [] indicates the maximum number of mice in front of the mouse used to store the I-th mouse.
Int G [1005]; // G [] is used to store the serial number of a mouse before the first mouse.
Bool CMP (Mouse A, Mouse B)
{
If (A. W = B. W) return a. s> B. S;
Else return A. W <B. W;
}
Int main ()
{
Int I = 0, J, N, max = 0, flag = 0;
While (scanf ("% d", & M [I]. W, & M [I]. s )! = EOF)
M [I]. No = I + 1;
I ++;
}
N = I;
Sort (M, m + n, CMP );
For (I = 1; I <n; I ++)
{
F [I] = 1;
For (j = I-1; j> = 0; j --)
{
If (M [I]. w> M [J]. W & M [I]. S <m [J]. S & F [J] + 1> F [I]) // increases the speed and quantity of important objects.
{
F [I] = 1 + F [J];
G [I] = J;
}
}
If (max <F [I]) // The maximum number of rats at a time, and records the order number of the mouse with the largest number.
{
Max = f [I];
Flag = I;
}
}
 
For (I = 0, j = max-1; I <Max; I ++) // invert the order
{
F [j --] = flag;
Flag = G [flag];
}
Printf ("% d \ n", max );
For (I = 0; I <Max; I ++)
Printf ("% d \ n", M [f [I]. No); // output the mouse number
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.