HDU 1160 FatMouse's Speed(DP)

点我看题目

题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置。

思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题目是什么意思。其实就是先按照体重排序,然后在速度里边找最长下降子序列,记录一下他们原来的位置,输出就行。数组开小了还WA了一次

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>

using namespace std ;

struct node
{
    int weight ;
    int speed ;
    int position ;
}map[110] ;

int pre[1100] ;
int dp[1100] ;

bool cmp(const node a,const node b)
{
    if(a.weight == b.weight) return a.speed > b.speed ;
    return a.weight < b.weight ;
}

int main()
{
    int i = 1 ;
    while(scanf("%d %d",&map[i].weight,&map[i].speed) != EOF)
    {
        map[i].position = i ;
        i++ ;
    }
    sort(map+1,map+i,cmp) ;
    pre[1] = 1 ;
    dp[1] = 1 ;
    stack<int>Q ;
    for(int j = 2 ; j < i ; j++)
    {
        int t = 0 ;
        for(int k = 1 ; k < j ; k++)
        {
            if(map[k].weight < map[j].weight && map[k].speed > map[j].speed && dp[k] > t)
            {
                t = dp[j] ;
                pre[j] = k ;
            }
            dp[j] = t+1 ;
        }
    }
    int maxx = -1,maxn ;
    for(int j = 1 ; j < i ; j++)
    {
        if(dp[j] > maxx)
        {
            maxx = dp[j] ;
            maxn = j ;
        }
    }
    printf("%d
",maxx) ;
    int n = maxx ;
    while(maxx--)
    {
        int t = maxn ;
        Q.push(map[t].position) ;
        maxn = pre[t] ;
    }
    while(n--)
    {
        printf("%d
",Q.top()) ;
        Q.pop() ;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3601229.html