http://acm.hdu.edu.cn/showproblem.php?pid=1160
哎,刚才不注意,数组开成了100的,WA了两次。。。
这个题目是最长有序序列,先按weight为第一关键字,speed为第二关键字排序,再以speed为关键字求最长下降子序列。
1 #include <iostream>
2 #include <algorithm>
3 #include <stack>
4 #define MAX 1000
5 using namespace std;
6 struct mice
7 {
8 int index;
9 int weight;
10 int speed;
11 };
12 mice mices[MAX+10];
13 int f[MAX+10];
14 int flag[MAX+10];
15 bool cmp(const mice mice1,const mice mice2)
16 {
17 if(mice1.weight!=mice2.weight)
18 {
19 return mice1.weight < mice2.weight;
20 }
21 else
22 {
23 return mice1.speed>mice2.speed;
24 }
25
26 }
27 int main()
28 {
29 int w,s;
30 stack <int> stk;
31 int cnt=1;
32 f[1]=1;
33 flag[1]=1;
34 //input
35 while(cin>>w>>s)
36 {
37 mices[cnt].index = cnt;
38 mices[cnt].weight =w;
39 mices[cnt].speed =s;
40 cnt++;
41 }
42 //sort
43 sort(mices+1,mices+cnt,cmp);
44
45 //dp
46 for(int i=2;i<cnt;i++)
47 {
48 int maxTemp;
49 int temp=0;
50 for(int j=1;j<i;j++)
51 {
52 if(mices[i].weight>mices[j].weight&& mices[i].speed<mices[j].speed)
53 {
54 if(temp<f[j])
55 {
56 temp = f[j];
57 flag[i]=j;
58 }
59 }
60 }
61 f[i]=temp+1;
62 }
63 int max=-1;
64 int maxi;
65 for(int i=1;i<cnt;i++)
66 {
67 if(f[i]>max)
68 {
69 max = f[i];
70 maxi = i;
71 }
72 }
73 cout<<max<<endl;
74 for(int i=0;i<max;i++)
75 {
76 int j=maxi;
77 stk.push(mices[j].index);
78 maxi = flag[j];
79 }
80 while(!stk.empty())
81 {
82 cout<<stk.top()<<endl;
83 stk.pop();
84 }
85 return 0;
86 }
87
2 #include <algorithm>
3 #include <stack>
4 #define MAX 1000
5 using namespace std;
6 struct mice
7 {
8 int index;
9 int weight;
10 int speed;
11 };
12 mice mices[MAX+10];
13 int f[MAX+10];
14 int flag[MAX+10];
15 bool cmp(const mice mice1,const mice mice2)
16 {
17 if(mice1.weight!=mice2.weight)
18 {
19 return mice1.weight < mice2.weight;
20 }
21 else
22 {
23 return mice1.speed>mice2.speed;
24 }
25
26 }
27 int main()
28 {
29 int w,s;
30 stack <int> stk;
31 int cnt=1;
32 f[1]=1;
33 flag[1]=1;
34 //input
35 while(cin>>w>>s)
36 {
37 mices[cnt].index = cnt;
38 mices[cnt].weight =w;
39 mices[cnt].speed =s;
40 cnt++;
41 }
42 //sort
43 sort(mices+1,mices+cnt,cmp);
44
45 //dp
46 for(int i=2;i<cnt;i++)
47 {
48 int maxTemp;
49 int temp=0;
50 for(int j=1;j<i;j++)
51 {
52 if(mices[i].weight>mices[j].weight&& mices[i].speed<mices[j].speed)
53 {
54 if(temp<f[j])
55 {
56 temp = f[j];
57 flag[i]=j;
58 }
59 }
60 }
61 f[i]=temp+1;
62 }
63 int max=-1;
64 int maxi;
65 for(int i=1;i<cnt;i++)
66 {
67 if(f[i]>max)
68 {
69 max = f[i];
70 maxi = i;
71 }
72 }
73 cout<<max<<endl;
74 for(int i=0;i<max;i++)
75 {
76 int j=maxi;
77 stk.push(mices[j].index);
78 maxi = flag[j];
79 }
80 while(!stk.empty())
81 {
82 cout<<stk.top()<<endl;
83 stk.pop();
84 }
85 return 0;
86 }
87