HDOJ 1160 FatMouse’s Speed解題報告

來源:互聯網
上載者:User
//#include <fstream>#include<iostream>#include <memory>#include <algorithm>using namespace std;struct Mice{int weight;int speed;int pos;};int cmp( const void *a , const void *b ) { struct Mice *c = (Mice *)a; struct Mice *d = (Mice *)b; if(c->weight >d->weight) return 1; else if(c->weight <d->weight) return -1; else return 0;} int FatMouseSpeed(int len,int* traceLength,int* trace,Mice* m){qsort(m,len,sizeof(Mice),cmp);int max=0;for(int i=1;i<len;i++){for(int temp=0;temp<i;temp++){if(m[i].weight>m[temp].weight&&m[i].speed<m[temp].speed){if(traceLength[i]<traceLength[temp])//這裡錯的太糾結{trace[i]=temp;traceLength[i]=traceLength[temp];}}}traceLength[i]++;if(traceLength[i]>traceLength[max])max=i;}return max;}void output(Mice* m,int* trace,int*traceLength,int pos){if(traceLength[pos]!=1)output(m,trace,traceLength,trace[pos]);cout<<m[pos].pos<<endl;}int main(){//fstream cin("FatMouseSpeed.txt");int len=0;int trace[1001]={0};int traceLength[1001]={1};Mice mice[1001];while(cin>>mice[len].weight>>mice[len].speed){mice[len].pos=len+1;len++;}int pos=FatMouseSpeed(len,traceLength,trace,mice);cout<<traceLength[pos]<<endl;output(mice,trace,traceLength,pos);}

總結與教訓:

1、第一個用動態規劃完成的題目O(∩_∩)O哈哈~

2、忘記賦值之前的判斷了,結果悲劇WA,算是第一次使用動態規劃的一個小教訓吧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.