hdu 1160 上升序列 dp

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23350    Accepted Submission(s): 10391
Special Judge


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 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
题意::给你许多组数据,每一组两个数,一个代表老鼠的重量,一个代表老鼠的速度,为了证明老鼠越重速度越慢,让你取出几组数据证明,问最多能取出几组。
 
思路:定义dp[i],表示以第i个数据结尾,符合题目要求的序列长度为dp[i],开一个pre数组,用来记录上一个符合要求的数据的下标,再开一个ans数组记录排序之后符合要求的数据的下标,
初始时让p[i].num=i,输出时用ans数组将排序前后的数据下标统一起来输出答案
 
 
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
  int w;
  int v;
  int num;
}p[1005];
bool cmp(node a,node b)//重量递增,速度递减
{
  if(a.w<b.w)
    return a.w<b.w;
  else if(a.w==b.w&&a.v>b.v)
    return a.v>b.v;
  else
    return 0;
}
int dp[1005],pre[1005],ans[1005];
//dp[i]表示以第i个数据结尾的符合要求的子序列长度
//pre[i]记录i对应的上一个数据的下标(滚动数组,这里的上一个是指符合要求的数据,所以,pre[i]!=dp[i-1])
//ans[i]存放符合要求的数据的下标(排序之后的下标,输出的时候要对应回去)
int main()
{
  int i=0;
  while(~scanf("%d%d",&p[i].w,&p[i].v))
  {
    p[i].num=i;
    dp[i]=1;
    pre[i]=-1;
    i++;
  }
  int n=i,len=0,x;//len最长序列长度,x最长序列长度的下标
  sort(p,p+n,cmp);

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<i;j++)
    {
      if(p[i].w>p[j].w&&p[i].v<p[j].v&&dp[j]+1>dp[i])
      {
        dp[i]=dp[j]+1;
        pre[i]=j;
        if(dp[i]>len)
        {
          len=dp[i];
          x=i;
        }
      }
    }
  }
  int k=0;
  while(x!=-1)
  {
    ans[k++]=x;
    x=pre[x];
  }
  printf("%d
",k);//输出最长序列长度
  for(int i=k-1;i>=0;i--)
  {
    printf("%d
",p[ans[i]].num+1);
  }
  return 0;
}
原文地址:https://www.cnblogs.com/-citywall123/p/11004154.html