[FatMouse's Speed] LIS

Description

HDU-1160

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.

Solution

先按照重量排序,再求出最长上升子序列,记录模板在此。

Code

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define max(a,b) a>b?a:b
#define inf 0x3f3f3f3f
int dp[1050];

struct Mouse{
    int w,s,n,l;
}m[1050];

int cmp(Mouse m1,Mouse m2){
    if(m1.w==m2.w)return m1.s<m2.s;
    return m1.w>m2.w;
}

int main(){
    int c=1,i,j;
    while(scanf("%d%d",&m[c].w,&m[c].s)!=EOF){
        m[c].n=c;
        m[c].l=0;
        c++;
    }
    sort(m+1,m+c+1,cmp);
    m[0].w=inf;
    m[0].s=0;
    m[0].l=0;
    m[0].n=0;
    int ans=0;
    for(i=1;i<=c;i++){
        dp[i]=0;
        for(j=0;j<i;j++){
            if(m[j].w>m[i].w&&m[j].s<m[i].s){
                if(dp[j]+1>=dp[i]){
                    dp[i]=dp[j]+1;
                    m[i].l=j;
                    if(dp[i]>dp[ans])ans=i;
                }
            }
        }
    }
    printf("%d\n",dp[ans]);
    while(m[ans].l!=0){
        printf("%d\n",m[ans].n);
        ans=m[ans].l;
    }
    printf("%d\n",m[ans].n);
    return 0;
}

[FatMouse's Speed] LIS

原文地址:https://www.cnblogs.com/ez4zzw/p/12600266.html

时间: 2024-10-04 21:43:59

[FatMouse's Speed] LIS的相关文章

HDU FatMouse&#39;s Speed (LIS)

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

动态规划(DP),类似LIS,FatMouse&#39;s Speed

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 解题报告: 1.首先按照weight从小到大排列,weight相同的按照speed从大到小排列; 2.Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度: 3.path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置 4.递归输出id void output(in

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

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

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

hdu(1160)——FatMouse&#39;s Speed

题意: 现在给你一些数据输入,第一个代表的是体积,第二个代表的是它的速度.然后题目让你找到当体积递增但是速度递减时的最长的一个子序列,注意这里体积是要严格的递增的,速度则是要严格的递减的.最后要你把他们的序号输出. 思路: 是不是有点感觉像LIS问题.但是这里还是有点差别的.因为它要让你记录. 首先我们先对体积从小到大排序,然后我们对速度进行最长递减子序列的查询. 这里的记录前驱和我们上次的那个迷宫问题(poj 3984)一样,只需要另外开一个数组就好了. 链接:http://blog.csdn

hdu1160,FatMouse&#39;s Speed

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

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

(最长上升子序列 并记录过程)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