FatMouse'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]] (速度)

分析:有两个变量的话比较不好控制,自然需要先排序。再仔细思考的话,觉得和之前做的防御导弹有点类似,都是求最多能有几个符合条件的。思考到这,能       求出来了,然后再记录路径就可以了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 4007;

typedef long long LL;
int a[maxn], dp[maxn];

struct node
{
    int w, v, sign, h;
}s[maxn];

///按照体重从大到小的顺序排序,若体重相同,按照速度从小到大排序
bool cmp(node p, node q)
{
    if(p.w == q.w)
        return p.v<q.v;

    return p.w>q.w;
}

int main()
{
    int k=1;

    while(scanf("%d %d", &s[k].w, &s[k].v)!=EOF)
    {
        s[k].h = k;
        s[k].sign = -1;
        k++;
    }

    sort(s+1, s+k+1, cmp);

        int ans = 0;

    for(int i=1; i<k; i++)
    {
        dp[i] = 1;///不管怎样,它本身都符合条件,也就是它自己1个
        for(int j=1; j<i; j++)
        {
           if(s[i].w<s[j].w && s[i].v>s[j].v)
           {
               if(dp[j]+1>=dp[i])
               {
                   dp[i] = dp[j]+1;
                   s[i].sign = j;///标记路径
               }
           }

           if(dp[i]>dp[ans])
               ans = i;

        }
    }

    printf("%d\n", dp[ans]);

    while(s[ans].sign!=-1)
    {
        printf("%d\n", s[ans].h);
        ans = s[ans].sign;
    }

    printf("%d\n", s[ans].h);

    return 0;
}

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

时间: 2024-08-26 10:22:17

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

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

FatMouse&#39;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; retu

hdu 1160 排序 + 最长上升子序列

题意: 输出体重上升而速度下降的最长子序列 题意: 先按照结构体升序排序体重,之后用dp对速度求最长下降子序列即可. 代码: #include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <string> #include <vector> #include <cstdio> #include

uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)

本题难处好像是在于 可以把一些灯泡换成电压更高的灯泡以节省电源的钱 ,所以也才有了对最优方案的探求 好的处理方法是按照电压从小到大排序,只能让前面的换成后面的,也就满足了把一些灯泡换成电压更高的灯泡 的要求: 一种电压的灯泡,要么不换,要换则应该全换:换,说明用当前的电源不值:而既然不值则应该全部换掉以避免使用当前电源,不然即增加了灯泡费用又没节省电源费用,亏大了... 状态转移详见代码 #include<cstdio> #include<cstring> #include<

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

动态规划-最长公共子序列

(1).问题描述:给出2个序列,x是从1到m,y是从1到n,找出x和y的最长公共子序列? x:A B C B D A B y:B D C A B A 则:最长公共子序列长度为4,BDAB BCAB BCBA均为LCS(最长公共子序列): 模型实现图: (2).问题解决 代码实现了最长公共子序列的长度 #include<stdio.h> #define N    10 int LCS(int *a, int count1, int *b, int count2); int LCS(int *a,

动态规划 - 最长公共子序列(LCS)

最长公共子序列也是动态规划中的一个经典问题. 有两个字符串 S1 和 S2,求一个最长公共子串,即求字符串 S3,它同时为 S1 和 S2 的子串,且要求它的长度最长,并确定这个长度.这个问题被我们称为 最长公共子序列问题. 与求最长递增子序列一样,我们首先将原问题分割成一些子问题,我们用 dp[i][j]表示 S1 中前 i 个字符与 S2 中前 j 个字符分别组成的两个前缀字符串的最 长公共子串长度. 显然的,当 i. j 较小时我们可以直接得出答案,如 dp[0][j]必 等于 0.那么,

动态规划---最长上升子序列问题(O(nlogn),O(n^2))

LIS(Longest Increasing Subsequence)最长上升子序列 或者 最长不下降子序列.很基础的题目,有两种算法,复杂度分别为O(n*logn)和O(n^2) . ********************************************************************************* 先回顾经典的O(n^2)的动态规划算法: 设a[t]表示序列中的第t个数,dp[t]表示从1到t这一段中以t结尾的最长上升子序列的长度,初始时设dp[