HDOJ 1160 FatMouse's Speed

  第一个用动态规划完成的题目O(∩_∩)O哈哈~。忘记赋值之前的判断了,结果悲剧WA,算是第一次使用动态规划的一个小教训吧。

View Code
 1 //#include <fstream>   
2 #include<iostream>
3 #include <memory>
4 #include <algorithm>
5 using namespace std;
6 struct Mice
7 {
8 int weight;
9 int speed;
10 int pos;
11 };
12 int cmp( const void *a , const void *b )
13 {
14 struct Mice *c = (Mice *)a;
15 struct Mice *d = (Mice *)b;
16 if(c->weight >d->weight) return 1;
17 else if(c->weight <d->weight) return -1;
18 else return 0;
19 }
20 int FatMouseSpeed(int len,int* traceLength,int* trace,Mice* m)
21 {
22 qsort(m,len,sizeof(Mice),cmp);
23 int max=0;
24 for(int i=1;i<len;i++)
25 {
26 for(int temp=0;temp<i;temp++)
27 {
28 if(m[i].weight>m[temp].weight&&m[i].speed<m[temp].speed)
29 {
30 if(traceLength[i]<traceLength[temp])//这里错的太纠结
31 {
32 trace[i]=temp;
33 traceLength[i]=traceLength[temp];
34 }
35 }
36 }
37 traceLength[i]++;
38 if(traceLength[i]>traceLength[max])
39 max=i;
40
41 }
42 return max;
43 }
44 void output(Mice* m,int* trace,int*traceLength,int pos)
45 {
46 if(traceLength[pos]!=1)
47 output(m,trace,traceLength,trace[pos]);
48 cout<<m[pos].pos<<endl;
49 }
50 int main()
51 {
52 //fstream cin("FatMouseSpeed.txt");
53 int len=0;
54 int trace[1001]={0};
55 int traceLength[1001]={1};
56 Mice mice[1001];
57 while(cin>>mice[len].weight>>mice[len].speed)
58 {
59 mice[len].pos=len+1;
60 len++;
61 }
62 int pos=FatMouseSpeed(len,traceLength,trace,mice);
63 cout<<traceLength[pos]<<endl;
64 output(mice,trace,traceLength,pos);
65 }



posted on 2011-07-31 21:55  AdaByron  阅读(208)  评论(0编辑  收藏  举报

导航