標籤:style blog http color os 資料
半個下午,總算A過去了
畢竟水題
好歹是自己獨立思考,debug,然後2A過的
我為人人的dp演算法
題意:
為了支援你的觀點,你需要從給的資料中找出盡量多的資料,說明老鼠越重速度越慢這一論點
本著“指標是程式員殺手”這一原則,我果斷用pre來表示這隻老鼠的直接前驅的序號
代碼中我是按體重從大到小排序,然後找出一條最長的體重嚴格遞減速度嚴格遞增的“鏈”(其實找到的是鏈尾)。
然後輸出的時候從後往前輸出
對結構體排序
對於範例來說,迴圈完以後應該是這樣的:
| order |
2 |
3 |
4 |
8 |
1 |
5 |
7 |
0 |
6 |
| weight |
500 |
1000 |
1100 |
2000 |
6000 |
6000 |
6000 |
6008 |
8000 |
| speed |
2000 |
4000 |
3000 |
1900 |
2100 |
2000 |
1200 |
1300 |
1400 |
| lenth |
1 |
1 |
2 |
3 |
3 |
3 |
4 |
4 |
4 |
| pre |
-1 |
-1 |
3 |
4 |
4 |
4 |
8 |
8 |
8 |
pre == -1代表沒有前驅。
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 struct Mouse 9 {10 int order;11 int weight;12 int speed;13 int pre;14 int lenth;15 }mice[1010];16 17 int a[1010];18 19 bool cmp1(Mouse a, Mouse b)20 {21 if(a.weight != b.weight)22 return (a.weight > b.weight);23 return (a.speed < b.speed);24 }25 26 bool cmp2(Mouse a, Mouse b)27 {28 return (a.order < b.order);29 }30 31 int main(void)32 {33 #ifdef LOCAL34 freopen("1160in.txt", "r", stdin);35 #endif36 37 int cnt = 0;38 int w, s;39 while(scanf("%d%d", &w, &s) == 2)40 {41 mice[cnt].weight = w;42 mice[cnt].speed = s;43 mice[cnt].order = cnt;44 ++cnt;45 }46 47 sort(mice, mice+cnt, cmp1);48 int i, j;49 for(i = 0; i < cnt; ++i)50 {51 mice[i].lenth = 1;52 mice[i].pre = -1;53 }54 //我為人人55 for(i = 1; i < cnt; ++i)56 {57 for(j = 0; j < i; ++j)58 {59 if(mice[j].weight > mice[i].weight 60 && mice[j].speed < mice[i].speed61 && mice[j].lenth+1 > mice[i].lenth)62 {63 mice[i].lenth = mice[j].lenth + 1;64 mice[i].pre = mice[j].order;65 }66 }67 }68 69 sort(mice, mice+cnt, cmp2);70 int maxlen = 0;71 for(i = 1; i < cnt; ++i)72 if(mice[i].lenth > mice[maxlen].lenth)73 maxlen = i;74 int l = mice[maxlen].lenth;75 printf("%d\n%d\n", l, maxlen+1);76 77 int p = maxlen;78 for(i = 1; i < l; ++i)79 {80 printf("%d\n", mice[p].pre+1);81 p = mice[p].pre;82 }83 return 0;84 }代碼君