HDU 1160 FatMouse's Speed(DP)

时间:2022-06-09 14:44:26

点我看题目

题意 :给你好多只老鼠的体重和速度,第 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[] ; int pre[] ;
int dp[] ; 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 = ;
while(scanf("%d %d",&map[i].weight,&map[i].speed) != EOF)
{
map[i].position = i ;
i++ ;
}
sort(map+,map+i,cmp) ;
pre[] = ;
dp[] = ;
stack<int>Q ;
for(int j = ; j < i ; j++)
{
int t = ;
for(int k = ; 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+ ;
}
}
int maxx = -,maxn ;
for(int j = ; j < i ; j++)
{
if(dp[j] > maxx)
{
maxx = dp[j] ;
maxn = j ;
}
}
printf("%d\n",maxx) ;
int n = maxx ;
while(maxx--)
{
int t = maxn ;
Q.push(map[t].position) ;
maxn = pre[t] ;
}
while(n--)
{
printf("%d\n",Q.top()) ;
Q.pop() ;
}
return ;
}