题意:
有N头象,找出其中体重从小到大,智商从高到底的X只,并输出。
思路:
先对于象的体重进行从小到大排序,然后对其智商进行最长下降子序列选择
#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <algorithm> using namespace std; struct Node { int i; int w, s; Node(int _w, int _s, int _i) : w(_w), s(_s), i(_i) { } friend bool operator < (const Node &a, const Node &b) { return a.w < b.w; } }; vector<Node> ele; int dp[1010], pre[1010]; int main() { int w, iq; int id = 0; while (scanf("%d %d", &w, &iq) != EOF) { ele.push_back(Node(w, iq, id + 1)); ++id; } ele.push_back(Node(0, 0, 0)); sort(&ele[0], &ele[id]); memset(dp, 0, sizeof(dp)); memset(pre, 0, sizeof(pre)); int mm, pos; dp[0] = 1, pre[0] = -1; mm = 1, pos = 0; for (int i = 1; i < id; ++i) { dp[i] = 1; for (int j = 0; j < i; ++j) { if (ele[i].s < ele[j].s && ele[i].w > ele[j].w && dp[i] <= dp[j]) dp[i] = dp[j] + 1, pre[i] = j; if (mm <= dp[i]) mm = dp[i], pos = i; } } vector<int> p; for (int i = 0; i < mm; ++i) p.push_back(ele[pos].i), pos = pre[pos]; printf("%d\n", mm); for (int i = mm - 1; i >= 0; --i) printf("%d\n", p[i]); return 0; }