会员
众包
新闻
博问
AI培训
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
windmissing
博客园
首页
新随笔
管理
hdu1160最长递增子序列
//1160 #include <iostream> #include <algorithm> using namespace std; struct mouse { int id; int weight; int speed; int pos; int pre; }s[10005]; bool cmp(mouse a,mouse b) { return(a.weight<b.weight||(a.weight==b.weight&&a.speed<b.speed)); } int main() { int count=0; while(scanf("%d %d",&s[count].weight,&s[count].speed)!=EOF) { s[count].id=count+1; count++; } sort(s,s+count,cmp); /*********************************************************************************/ //最长递增子序列部分 int ans[10005]={0}; s[0].pos=1; int i,j,max=1,maxi=0; //记录每个点的序列位置 for(i=1;i<count;i++) { j=max; while(j) { if(s[i].weight>s[ans[j]].weight&&s[i].speed<s[ans[j]].speed) { s[i].pos=j+1; ans[j+1]=i; s[i].pre=ans[j]; break; } j--; } //第一个点 if(j==0) { s[i].pos=1; ans[1]=i; } //目前为止最长的点 if(s[i].pos>max) { max=s[i].pos; maxi=i; } } /* for(int cc=0;cc<count;cc++) cout<<cc<<' '<<s[cc].id<<' '<<s[cc].weight<<' '<<s[cc].speed<<' '<<s[cc].pos<<' '<<s[cc].pre<<endl; */ cout<<max<<endl; //输出最长序列 int temp[10005]; for(i=1;i<=max;i++) { temp[i]=s[maxi].id; maxi=s[maxi].pre; } for(i=max;i>0;i--) cout<<temp[i]<<endl; /*********************************************************************************/ return 0; }
posted @
2011-12-29 19:03
windmissing
阅读(
140
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部