HDU 1160 fatmouse's speed (the longest single-byte sub-sequence DP)

Source: Internet
Author: User

Answer: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1160


Problem description

Fatmouse 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.

Input

Input 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.

Output

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 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 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample output

4
4
5
9
7

The token generation is as follows:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <algorithm>#define MAXN 10005#define RST(N)memset(N, 0, sizeof(N))using namespace std;typedef struct Node_{    int weight;    int speed;    int Num;}Node;Node N[MAXN];int f[MAXN], p[MAXN];int Max, position, i;int cmp(const void *a, const void *b){    Node *p1 = (Node *)a;    Node *p2 = (Node *)b;    if(p1->weight != p2->weight) return p1->weight - p2->weight;    else return p2->speed - p1->speed;}void solve(){    for(int j=1; j<i+1; j++) {        for(int k=1; k<j; k++) {            if(N[k].weight < N[j].weight && N[k].speed > N[j].speed) {                if(f[k]+1 > f[j]) {                    f[j] = f[k] + 1;                    p[j] = k;                    if(f[j] > Max) {                        Max = f[j];                        position = j;                    }                }            }        }    }    printf("%d\n", Max);}void display(int position){    if(p[position] == position) {        printf("%d\n", N[position].Num);        return ;    }    display(p[position]);    printf("%d\n", N[position].Num);}int main(){    i = 1, Max = 0, position = 0;    while(scanf("%d %d", &N[i].weight, &N[i].speed) != EOF) {        f[i] = 1;        p[i] = i;        N[i++].Num = i;    }    qsort(N+1, i, sizeof(Node), cmp);    solve(), display(position);    return 0;}


HDU 1160 fatmouse's speed (the longest single-byte sub-sequence DP)

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.