The body weight and speed of N rats are found from the longest sequence. The weight increases progressively and the speed decreases.
The simple DP order d [I] indicates the length of the last time in the sequence obtained by the I mouse. For each mouse I traverse all mice J when (W [I]> W [J]) & (s [I] <s [J]) Where d [I] = max (d [I], d [J] + 1) write down the last recursion in the output path.
#include<cstdio>#include<algorithm>using namespace std;const int M=1005;int w[M], s[M], d[M], pre[M], n, key;int dp (int i){ if (d[i] > 0) return d[i]; for (int j = d[i] = 1; j <= n; ++j) if ( (w[i] > w[j]) && (s[i] < s[j]) && (d[i] < dp (j) + 1)) d[i] = d[j] + 1, pre[i] = j; return d[i];}void print (int i){ if (pre[i]) print (pre[i]); printf ("%d\n", i);}int main(){ n = 0; while (scanf ("%d %d", &w[n], &s[++n]) != EOF); for (int i = key =1; i <= n; ++i) if (dp (i) > dp (key)) key = i; printf ("%d\n", d[key]); print (key); return 0;}
Fatmouse's speed
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 specified correct outputs for a given input, your program only needs to find one.
Sample Input
6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample output
44597