HDOJ_1160:FatMouse's Speed 解题报告

来源:互联网 发布:金融大数据 编辑:程序博客网 时间:2024/05/29 13:05

先对输入的数据按重量作第一关键字、速度作第二关键字进行排序,注意实际排序函数中是先处理第二关键字。本题从前向后递推就好,不必反序。ppt里面有个描述个人觉得有一定误导性,“设f[i]为Mice[i]至Mice[n]最长的序列长度。” 我看来,f[i]是从Mice[1]开始、以Mice[i]为结尾时,最长的满足单调条件的序列长度。

if( m[j].w!=m[i].w && m[j].s>m[i].s && m[j].length+1>m[i].length)

    m[i].length = m[j].length+1;  //此为递推关系

// HDOJ_1160.cpp : FatMouse's Speed#include <iostream>#include <algorithm>using namespace std;struct MICE{int w,s;int length,index;int pre;};bool cmp( MICE x, MICE y ){if( x.w==y.w )return x.s > y.s;elsereturn x.w < y.w;}int main(){MICE m[1001];int i=1, j, maxLength=1, maxIndex=1;while( cin >> m[i].w >> m[i].s )    {m[i].index = i;m[i].length = 1;m[i].pre = 0;i++;    }    //cout<<i<<endl;int n = i-1;//记录老鼠数量sort(m,m+n,cmp);for( i=2; i<=n; i++ )for( j=1; j<i; j++ )if( m[j].w!=m[i].w && m[j].s>m[i].s && m[j].length+1>m[i].length){ m[i].length = m[j].length+1;m[i].pre = j;}for( i=2; i<=n; i++ )if( maxLength<m[i].length ){maxLength = m[i].length;maxIndex = i;}cout << maxLength << endl;int *maxPath = new int[maxLength];maxPath[maxLength] = maxIndex;for( i=maxLength-1; i>0; i-- ){maxPath[i] = m[maxPath[i+1]].pre;if( maxPath[i]==0 )maxPath[i] = maxPath[i+1];}for( i=1; i<=maxLength; i++ )cout << m[maxPath[i]].index << endl;    return 0;}
原创粉丝点击