题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160
分析:结构体内用一个元素记录每个老鼠的初始编号,然后对体重进行排序,对速度dp(注意体重不相等);
结构体内再用一个元素记录初始编号为i的老鼠在最长序列中上一个是初始编号为j的老鼠
1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functional> 9 #include<iomanip> 10 #include<numeric> 11 #include<cmath> 12 #include<queue> 13 #include<vector> 14 #include<set> 15 #include<cctype> 16 #define PI acos(-1.0) 17 const int INF = 0x3f3f3f3f; 18 const int NINF = -INF - 1; 19 const int maxn = 1e3 + 5; 20 typedef long long ll; 21 using namespace std; 22 int num = 0, ans = 0, flag = -1; 23 int rec[maxn]; 24 struct node 25 { 26 int id, grams, speed; 27 int path; 28 }a[maxn]; 29 int dp[maxn]; 30 bool cmp(const node &a, const node &b) 31 { 32 return a.grams < b.grams; 33 } 34 int main() 35 { 36 while (scanf("%d %d", &a[num].grams, &a[num].speed) != EOF) 37 { 38 a[num].id = num + 1; 39 num++; 40 } 41 memset(rec, 0, sizeof(rec)); 42 sort(a, a + num, cmp); 43 //for (int i = 0; i < num; ++i) cout << a[i].id << ' ' << a[i].grams << ' ' << a[i].speed << endl; 44 for (int i = 0; i < num; ++i) dp[i] = 1; 45 for (int i = 0; i < num; ++i) 46 { 47 for (int j = 0; j < i; ++j) 48 { 49 if (a[j].grams != a[i].grams && a[j].speed > a[i].speed) 50 { 51 if (dp[j] + 1 > dp[i]) 52 { 53 dp[i] = dp[j] + 1; 54 a[a[i].id].path = a[j].id;//a[i].path是排序后而不是初始编号 55 } 56 } 57 } 58 if (dp[i] > ans) 59 { 60 ans = dp[i]; 61 flag = a[i].id; 62 } 63 } 64 //for (int i = 0; i < num; ++i) cout << dp[i] << ' '; 65 //cout << endl; 66 rec[ans] = flag; 67 for (int i = ans - 1; i >= 1; --i) 68 rec[i] = a[rec[i + 1]].path; 69 printf("%d ", ans); 70 for (int i = 1; i <= ans; ++i) 71 printf("%d ", rec[i]); 72 return 0; 73 }