HDOJ 1160 FatMouse's Speed 解题报告
动态规划,最长上升子序列。看代码吧,可能更容易懂。
#include <iostream> #include <algorithm> using namespace std; struct State { int id; int weight; int speed; int pre; int num; }; bool cmp(const State &a, const State &b) { if(a.weight!=b.weight) return a.weight<b.weight; else return a.speed>b.speed; } void output(int x, State a[]) { if(a[x].num!=1) output(a[x].pre, a); cout<<a[x].id<<endl; } int main() { State mouse[1001]; int i,j,max=0,n=0; while(cin>>mouse[n].weight>>mouse[n].speed) { mouse[n].num=1; mouse[n].id=n+1; mouse[n].pre=0; n++; } sort(mouse,mouse+n,cmp); for(i=1;i<n;i++) for(j=0;j<i;j++) if((mouse[i].num<mouse[j].num+1) && (mouse[i].weight>mouse[j].weight) && (mouse[i].speed<mouse[j].speed)) { mouse[i].num=mouse[j].num+1; mouse[i].pre=j; if(mouse[i].num>=mouse[max].num) max=i; } cout<<mouse[max].num<<endl; output(max,mouse); }
一开始笔者也是不会做的,网上看了大牛的代码。。。然后自己试着去理解,再自己写的代码。嗯,貌似比看书强。做了数塔,Max Sum,还有这题,对于动态规划还是一知半解,还得继续努力啊^_^