HDU 1160 dp中的路径问题

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 particular 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 should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should 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 should be as large as possible. 
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many 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

按照体重递增 速度递减的方式排序  然后找到最大的串   主要是路径问题
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define maxn 10005
#define N 3000
int dp[N],p[N],a[N];
struct node
{
    int w,s,t;
}q[N];
int cmp(node e,node f)
{
    if(e.w!=f.w) return e.w<f.w;
    return e.s>f.s;
}
int main()
{
    int n=1;
    while(scanf("%d%d",&q[n].w,&q[n].s)!=EOF)
    {
        q[n].t=n;
        dp[n]=1;
        p[n]=0;
        n++;
    }
    n=n-1;
    sort(q+1,q+1+n,cmp);
    int maxx=-12365478,m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<i;j++)
        {
            if(q[i].w>q[j].w&&q[i].s<q[j].s&&dp[i]<=dp[j])///dp[j]>=dp[i] 即dp[i]=dp[j]+1存进去才有意义
            {
                dp[i]=dp[j]+1;
                p[i]=j;///p[i]=j保留上一个的位置  方便依次查找
                if(dp[i]>=maxx)
                {
                    maxx=dp[i];
                    m=i;///保留最大值的位置
                }
            }
        }
    }
    int x=m,i=0;
    while(x)
    {
        a[i++]=x;///提取到数组
        x=p[x];///依次查找
    }
    printf("%d\n",i);
    while(i>0)
    {
        i--;
        printf("%d\n",q[a[i]].t);///输出
    }
    return 0;
}
时间: 2024-10-05 00:33:03

HDU 1160 dp中的路径问题的相关文章

HDU 1160 DP最长子序列

G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1160 Appoint description: Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you

hdu 1160 dp 入门

链接http://acm.hdu.edu.cn/showproblem.php?pid=1160 感觉也是最长上升子序列的变形... 这回独立1Y!开心~  不过在保存路径的时候调了一段时间orzzzzz还是太弱 思路:每个老鼠进行排序,将体重从小到大,若相等再将速度从大到小,保证找出最多的. 定义dp[i]表示以i为末尾的满足条件的最长的序列长度.运用最长上升子序列的那种方法就可以做了,还有要注意的是因为需要将其进行排序,排序后的序号是乱的,所以需要在结构体中加入一个记录原本路径的元素num.

HDU - 1160 FatMouse&#39;s Speed(dp+路径记录)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:给定x组老鼠的重量(W)和速度(S).求使得   W[m[1]] < W[m[2]] < ... < W[m[n]]       S[m[1]] > S[m[2]] > ... > S[m[n]] 的最长序列长度和路径 题解:排序一下,然后LIS,路径记录一下,输出就可以了 1 #include <iostream> 2 #include <a

[dp专题]hdu 1160 FatMouse&#39;s Speed

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10172    Accepted Submission(s): 4521Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster i

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed 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

HDU 1160 FatMouse&#39;s Speed DP题解

本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,不过因为需要按原来的标号输出,故此需要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法,基本的一维动态规划法了. 记录路径:只需要记录后继标号,就可以逐个输出了. #include <stdio.h> #include <algorithm> using namespace std; const int MAX_N = 1005; struct MouseSpeed { int id, w, s; b

HDU 1160 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了. 题目思路:这是个最长上升子序列的问题,我们按W的升序进行排序,若W相等则按V的降序排序.用Pre[]记录当前点的前驱节点,Last记录序列最后一个点,maxn记录最长长度,完成动规后可根据Last和Pre[]输出路径. #include<cstdio> #include<stdio.h&

HDU 1160 FatMouse&amp;#39;s Speed DP题解

本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录路径:仅仅须要记录后继标号,就能够逐个输出了. #include <stdio.h> #include <algorithm> using namespace std; const int MAX_N = 1005; struct MouseSpeed { int id, w, s;

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: 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 s