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

正常一个·变量的求法变为结构体封装,,,,,,额外加一个pre数组记录回溯路径

1A~~~~

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005;
struct node{
    int weight,speed,id;
}que[maxn];
int dp[maxn],pre[maxn];
bool cmp(struct node t1,struct node t2){
   if(t1.weight!=t2.weight)
    return t1.weight<t2.weight;
    return t1.speed>t2.speed;
}

int main(){
    int x,y;
    int tot;
    tot=1;
    while(scanf("%d%d",&que[tot].weight,&que[tot].speed)!=EOF){
        que[tot].id=tot;
        tot++;
    }
    sort(que+1,que+tot+1,cmp);
    memset(dp,0,sizeof(dp));
    memset(pre,-1,sizeof(pre));
    dp[1]=1;
    for(int i=2;i<tot;i++){
        dp[i]=1;
       for(int j=i-1;j>=1;j--){
           if((que[i].weight>que[j].weight)&&(que[i].speed<que[j].speed)&&dp[i]<dp[j]+1){
               dp[i]=dp[j]+1;
               pre[i]=j;
           }
       }
    }
     int point,ans=-1;
    for(int i=1;i<tot;i++){
          if(dp[i]>ans){
             ans=dp[i];
              point=i;
          }
    }
    printf("%d\n",ans);
    int tmp[maxn];
    int cnt=0;
    for(int i=point;i!=-1;i=pre[i]){
//       printf("%d\n",que[i].id);
        tmp[cnt++]=que[i].id;
    }
    for(int i=cnt-1;i>=0;i--)
    printf("%d\n",tmp[i]);

    return 0;
}
时间: 2024-09-30 02:20:16

HDU 1160 DP最长子序列的相关文章

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 入门

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

HDU 1069&amp;&amp;HDU 1087 (DP 最长序列之和)

H - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1087 Appoint description:  System Crawler  (2015-11-18) Description Nowadays, a kind of chess game called “Su

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, b

HDU 1160 FatMouse&#39;s Speed--dP--(元素1递增元素2递降的最长子序列)

题意:找到体重递增速度递降的最长序列 分析:和最长递增子序列一样,不过这里先做处理:先把体重按递增排序,然后找最长递降子序列即可 代码: #include<iostream> #include<algorithm> #include<cstdio> using namespace std; struct node{ int w,s; int t; }a[2000]; int n,i; struct h{ int x; int pre; }dp[2000]; int an

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+路径记录)

题目链接: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

单调递增最长子序列(南阳oj17)(经典dp)

单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理 随后的n行,每行有一个字符串,该字符串的长度不会超过10000 输出 输出字符串的最长递增子序列的长度 样例输入 3 aaa ababc abklmncdefg 样例输出 1 3 7 来源 经典题目 上传者 iphxer #include<std

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