欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 1160 FatMouse's Speed(最长不下降子序列)

发布时间:2024/4/11 编程问答 58 豆豆
生活随笔 收集整理的这篇文章主要介绍了 HDU - 1160 FatMouse's Speed(最长不下降子序列) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出许多二元组(W,S),最后要求输出最长的满足W严格递增,S严格递减的子序列长度,以及方案,输出任意一种即可

题目分析:看起来像二维偏序,其实对任意一维排序后求最长不下降子序列或最长不上升子序列即可,模板题,增加了个路径输出,在更新的时候保存一下即可,有点忘了板子了,贴一个一个月之前写的代码回顾回顾:

#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<sstream> #include<cmath> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e3+100;struct Node {int w,s;int pos; }a[N];bool cmp(Node a,Node b) {if(a.s==b.s)return a.w<b.w;return a.s>b.s; }int d[N];int c[N];int main() { // freopen("input.txt","r",stdin);int x,y;int cnt=1;while(scanf("%d%d",&x,&y)!=EOF){a[cnt].w=x;a[cnt].pos=cnt;a[cnt++].s=y;}sort(a+1,a+1+cnt,cmp);int len=1;memset(d,0,sizeof(d));d[1]=a[1].w;c[1]=1;for(int i=2;i<cnt;i++){if(a[i].w>d[len]){d[++len]=a[i].w;c[i]=len;}else{int j=upper_bound(d+1,d+1+len,a[i].w)-d;d[j]=a[i].w;c[i]=j;}}cout<<len<<endl;int j=len;stack<int>ss;for(int i=cnt-1;i>=1;i--){if(c[i]==j){ss.push(a[i].pos);j--;}}while(!ss.empty()){cout<<ss.top()<<endl;ss.pop();}return 0; }

 

总结

以上是生活随笔为你收集整理的HDU - 1160 FatMouse's Speed(最长不下降子序列)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。