HDOJ-1160-FatMouse's Speed:已AC,但留有疑惑【未解决】

#include<iostream>

#include<cstdio>

#include<fstream>

#include<algorithm>

#include<stack>

#include<cstring>

using namespace std;

#define Size 1000

struct Node

{

        int Weight, Speed;

        int index;

        bool operator < ( Node node )const{//最后这个const不可缺省 否则编译器报错 因为const对象调用非const成员函数非法

                return Weight < node.Weight;

        }

}mouse[Size+1];



int path[Size+1];

int table[Size+1];



int main()

{

        //ifstream cin("test.txt");

        int mice_num=1;

        while( cin>>mouse[mice_num].Weight>>mouse[mice_num].Speed ){

            mouse[mice_num].index=++mice_num;// 至今未理解 为何 mice_num++ WR···

        }



        sort(mouse+1, mouse+mice_num);



        int theLongest = 0;// 序列长度

        int theLastMouse; //  最长序列的最后一个老鼠

        memset(path, 0, sizeof(path));



        for( int i=1; i<mice_num; i++ ){// 基本动态转移过程

                table[i]=1;

                for( int j=1; j<i; j++ )

                        if( mouse[i].Weight>mouse[j].Weight && mouse[i].Speed<mouse[j].Speed && table[i]<table[j]+1 ){

                                table[i]=table[j]+1;

                                path[i]=j;

                        }

        }



        for( int i=1; i<mice_num; i++ )//遍历以求得 所要结果 当然也可以写进上一个 动态转移的循环中

            if( table[i]>theLongest )

            {

                    theLongest = table[i];

                    theLastMouse = i;

            }



        cout<<theLongest<<endl;

        stack<int>theSequence;// 由于自底向上的动态规划 求得最长序列 以及最后一个老鼠 而结果需要自顶向下输出 那么就利用堆栈罢

        while( table[theLastMouse]!=1 )

        {

                theSequence.push(mouse[theLastMouse].index);

                theLastMouse = path[theLastMouse];

        }

        theSequence.push(mouse[theLastMouse].index);

        while( !theSequence.empty() )

        {

                    cout<<theSequence.top()<<endl;

                    theSequence.pop();

        }

        return 0;

}

  

你可能感兴趣的:(SP)