FatMouse's Speed HDU - 1160 最长上升序列,

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int w, s;
    int index;  //储存标号
} mouse[1005]; //小鼠的信息
//先体重,后速度
bool cmp(node a,node b)
{
    if(a.w==b.w)
        return a.s<b.s;
    return a.w<b.w;
}
int main()
{
    int dp[1005],load[1005],ans[1005];
    int i, maxl,maxi,n,temp,x;
    i=1;
    while(scanf("%d%d",&mouse[i].w,&mouse[i].s)!= EOF)
    {
        mouse[i].index=i;
        dp[i]=1;
        load[i]=0;
        i++;
    }
    sort(mouse+1,mouse+i,cmp);
    //总数量
    n=i-1;
    maxl=0;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<i;j++)
            if(mouse[i].w>mouse[j].w&&mouse[i].s<mouse[j].s&&dp[i]<dp[j]+1)
            {
                //如果i的体重比j的小,而且速度j的大,并且还得j的最长子序列加一要大于目前i的最长子序列
                dp[i] = dp[j] + 1;
                load[i] = j;
            }
        if(dp[i]>maxl)
        {
            //最长序列
            maxl = dp[i];
            //序列最后一个
            maxi = i;
        }
    }
    temp=maxi;
    x=0;
    //将最终的结果标号存入数组中
    while(temp!=0)
    {
        ans[x]=temp;
        temp=load[temp];
        x++;
    }
    printf("%d\n", maxl);
    for(int i=maxl-1;i>= 0;i--)//标号存的时候是倒序,所以要给它反过来
        printf("%d\n",mouse[ans[i]].index);
    return 0;
}

FatMouse's Speed HDU - 1160 最长上升序列,

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12238364.html

时间: 2024-08-29 17:46:14

FatMouse's Speed HDU - 1160 最长上升序列,的相关文章

(最长上升子序列 并记录过程)FatMouse&#39;s Speed -- hdu -- 1160

http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12338    Accepted Submission(s): 5405Special Judge Problem Description FatMouse be

FatMouse&#39;s Speed hdu 1160(动态规划,最长上升子序列+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:现给出老鼠的体重与速度,要求你找出符合要求的最长子序列.       要求是 W[m[1]] < W[m[2]] < ... < W[m[n]](体重) && S[m[1]] > S[m[2]] > ... > S[m[n]] (速度) 分析:有两个变量的话比较不好控制,自然需要先排序.再仔细思考的话,觉得和之前做的防御导弹有点类似,都是求最多能有几个符合

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): 9174    Accepted Submission(s): 4061 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

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 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

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

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

HDU 1160 FatMouse&#39;s Speed (最长有序的上升子序列)

题意:给你一系列个w,s,要你找到最长的n使得 W[m[1]] < W[m[2]] < ... < W[m[n]] and S[m[1]] > S[m[2]] > ... > S[m[n]] 即在这n个w,s中满足w[i]<w[j]&&s[i]>s[j],要求:体重严格递增,速度严格递减,原始顺序不定 首先将s从大到小排序,即顺数固定后转化为最长上升子序列问题. 案例: 6008 1300 6000 2100 500 2000 1000 40

HDU 1160 FatMouse&#39;s Speed (最长上升子序列+记录路径)

题目链接:HDU 1160 FatMouse's Speed 题意:求体重越重,反而速度越慢的例子,并输出对应的编号. 对speed进行从大到小排序,再求weight的最长上升序列,并输出路径. AC代码: #include<stdio.h> #include<algorithm> #include<stack> using namespace std; struct Node { int weight; int speed; int id; }; struct Nod

HDU 1160 FatMouse&#39;s Speed (最长上升子序列)

题目链接 题意:n个老鼠有各自的重量和速度,要求输出最长的重量依次严格递增,速度依次严格递减的序列,n最多1000,重量速度1-10000. 题解:按照重量递增排序,找出最长的速度下降子序列,记录序列每个位置的左边的位置,找到最大值终点再递归输出即可.(好久没做题了,花了很多时间才AC.) #include <bits/stdc++.h> using namespace std; struct sa { int weight,speed,pos,l; } data[1005]; bool cm