Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1160
Give a bunch of mice, suppose there are N (enter n messages and then Ctrl + Z ). Each mouse has a corresponding weight and speed. Now we need to find the longest sequence from the N mouse sequences to meet the strict increasing weight of the mice and the strict decreasing speed.
We can use a struct to store mouse information, including weight, speed, and Id (this ID is before unordered, In order to output the final information ). We will first perform incremental sorting on weight. If the same weight is encountered, we will perform progressive descending sorting on speed. If weight is fixed, we can focus on processing speed, and the problem becomes the longest descending sequence. To save the path information, use path [] to save the code and output it recursively.
This question has been dragging on for a long time. It's almost a year ==, too lazy ~~~
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 10 struct node11 {12 int id;13 int weight, speed;14 }mouse[maxn];15 int path[maxn];16 int cnt[maxn];17 18 int cmp(node a, node b)19 {20 if (a.weight == b.weight)21 return a.speed > b.speed;22 return a.weight < b.weight;23 }24 25 void output(int pos)26 {27 if (!path[pos])28 return;29 output(path[pos]);30 printf("%d\n", path[pos]);31 }32 33 int main()34 {35 int w, sp, len = 1;36 #ifndef ONLINE_JUDGE37 freopen("in.txt", "r", stdin);38 #endif39 40 while (scanf("%d%d", &w, &sp) != EOF)41 {42 mouse[len].weight = w;43 mouse[len].speed = sp;44 mouse[len].id = len;45 cnt[len++] = -1;46 }47 sort(mouse+1, mouse+len, cmp);48 for (int i = 1; i < len; i++)49 {50 int k = 0;51 for (int j = 1; j < i; j++)52 {53 if (mouse[j].speed > mouse[i].speed && mouse[j].weight < mouse[i].weight && k < cnt[j])54 {55 k = cnt[j];56 path[mouse[i].id] = mouse[j].id;57 }58 }59 cnt[i] = k;60 cnt[i]++;61 }62 int ansid, ans = -1;63 for (int i = 1; i < len; i++)64 {65 if (cnt[i] > ans)66 {67 ans = cnt[i];68 ansid = mouse[i].id;69 }70 }71 printf("%d\n", ans);72 output(ansid);73 printf("%d\n", ansid);74 return 0;75 }
HDU 1160 fatmouse's speed problem solving report