FatMouse's Speed

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

思路:一道经典的最长子序列题,不过该题需要维护两个量,体重和速度,所以需要先对一个量进行排序,然后剩下的那个量就可以像处理最长子序列那样做了。 值得一提的是该题需要打印路径,最好的方法是用一个数组path运用类似链表的结构,来记录路径。 这恰恰就是紫书上数据结构那章例题14中所用的记录最短路路径的方法 。 其中的巧妙和实现细节请读者细细品味。 

针对这道题, 由于dp是利用之前计算的结果进行递推得到的,因此,每一步的计算都要用到上一步的结。最长上升子序列,就是枚举当前序列的最后一位,然后从前面递推找最优解, 所以这里的pre[i] = j;意思就是当前这个最优解i是由上一个最优解j递推出来的。所以可以用这种方法求的最优路径。  我无意中还找到了一个bug, 大家可以尝试一下1 2这组数据,也就是所有老鼠的体重和速度都一样,有些能AC的代码显然输出的不正确,因为他们没有输出路径。(为此我还system error了一发,还一度怀疑是不是oj的锅,后来试过才知道这只有一组数据的特例(Special judge))

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define mm(a) memset(a,0,sizeof(a))
const int N=10005;//题目没说多大数据,默认开大点
int path[N],dp[N];
using namespace std;
struct node
{
    int w,sp,num;//分别代表重量,速度,编号
} ans[N];
bool cmp(node x,node y)
{
    if(x.w==y.w)
        return x.sp<y.sp;
    return x.w>y.w;
}
int main()
{
    int sum,step,k,t,book;
    for(int i=1; i<N; i++)//path先记录自己的编号
        path[i]=i;
    k=1;
//freopen("C:\\Users\\nuoyanli\\Desktop\\DATA.txt","r",stdin);
    while(cin>>ans[k].w>>ans[k].sp)
    {
        ans[k].num=k;
        k++;
    }
    if(k==2)//Special judge
    {
        cout<<1<<endl<<1<<endl;
    }
    else
    {
        sort(ans+1,ans+k,cmp);
        mm(dp);
        sum=0;
        for(int i=1; i<k; i++)
        {
            step=0;
            for(int j=1; j<i; j++)
            {
                if(ans[i].w<ans[j].w&&ans[i].sp>ans[j].sp)
                {
                    if(step<dp[j])
                    {
                        step=dp[j];
                        book=ans[j].num;
                    }
                }
                if(step)
                    path[ans[i].num]=book;
                dp[i]=step+1;
                if(sum<dp[i])
                {
                    sum=dp[i];
                    t=ans[i].num;
                }
            }
        }
        cout<<sum<<endl;
        while(path[t]!=t)//输出path记录的编号
        {
            cout<<t<<endl;
            t=path[t];
        }
        cout<<t<<endl;//最后记录的是自己
    }
    return 0;
}