hdu1160簡單dp最長下降子序列

來源:互聯網
上載者:User
/*簡單dp,要記錄順序解:先排序,然後是一個最長下降子序列 ,中間需記錄順序dp[i]=Max(dp[i],dp[j]+1);*/#include<stdio.h>#include<string.h>#include<stdlib.h>#define N  1100/*w,s代表重量和速度,index記錄原來輸入時的順序下標,pre指向排序後的上一個下標,answer記錄排序後每一個位置的最優值*/typedef struct node {int w,s,index,pre,answer;}F;F f[N];int cmp(const void *a,const void *b) {return (*(F *)a).w-(*(F *)b).w;}void print(int k) {    if(f[k].pre==-1) {       printf("%d\n",f[k].index);       return;    }  print(f[k].pre);  printf("%d\n",f[k].index);}int main() {   int n=0,w,s,i,j,k,index;   while(scanf("%d%d",&f[n].w,&f[n].s)!=EOF)    f[n].index=n+1,n++;   qsort(f,n,sizeof(f[0]),cmp);   for(i=0;i<n;i++) {      //  printf("%d %d\n",f[i].w,f[i].s);     f[i].pre=-1;     f[i].answer=1;   for(j=0;j<i;j++)    if(f[i].w>f[j].w&&f[i].s<f[j].s&&f[i].answer<f[j].answer+1)        f[i].answer=f[j].answer+1,f[i].pre=j; //  printf("%d %d\n",f[i].answer,f[i].pre);   }   k=-1;   for(i=0;i<n;i++)    if(k<f[i].answer) {    k=f[i].answer;    index=//    }   printf("%d\n",k);//個數   print(index);//遞迴輸出要求的數順序return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.