HDU1160:FatMouse's Speed(最长上升子序列,不错的题)
生活随笔
收集整理的這篇文章主要介紹了
HDU1160:FatMouse's Speed(最长上升子序列,不错的题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:?http://acm.hdu.edu.cn/showproblem.php?pid=1160
學的東西還是不深入啊,明明會最長上升子序列,可是還是沒有A出這題,反而做的一點思路沒有,題意就不多說了,真是懷疑了為什么做這題的時候竟然想用nlog(n)的那個算法,
根本不可能有解啊,真想抽自己一下,最后無奈看了題解,發現他們寫的非常棒,記錄路徑僅用一個數組就可以實現,可惜我沒有想到啊,還是做題不深入啊。
首先需要排一下序。
代碼如下:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <math.h> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; int n,da[1010][1010],dp[1010]; struct node {int id,w,s; } q[1010]; int cmp(const void *a,const void *b) {struct node *aa=(struct node *)a;struct node *bb=(struct node *)b;if(aa->w!=bb->w)return aa->w-bb->w;else return bb->s-aa->s; } int main() {int tt,maxx=1;;tt=0;while(scanf("%d%d",&q[tt].w,&q[tt].s)!=EOF){if(q[tt].w==0) break;q[tt].id=tt+1;tt++;}qsort(q,tt,sizeof(q[0]),cmp);for(int i=0;i<tt;i++){da[i][1]=q[i].id;//da[i][j]數組用來保存以i開頭的最長序列,為j。dp[i]=1;//最短也包括它本身,為1,dp的初始化特別重要 }int x=0;for(int i=1;i<tt;i++){for(int j=0;j<i;j++){if(q[i].w>q[j].w&&q[i].s<q[j].s&&dp[j]+1>dp[i]){dp[i]=max(dp[i],dp[j]+1);for(int k=1;k<dp[i];k++){da[i][k]=da[j][k];}da[i][dp[i]]=q[i].id;//不斷更改路徑,大神們寫的真好}}if(dp[i]>maxx){maxx=dp[i];x=i;}}printf("%d\n",maxx);for(int i=1;i<=maxx;i++){printf("%d\n",da[x][i]);}return 0; }?
總結
以上是生活随笔為你收集整理的HDU1160:FatMouse's Speed(最长上升子序列,不错的题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国地区的官方ubuntu源
- 下一篇: [禅悟人生]清心寡欲, 才是人的真实写照