题意: 给出W数列和S数列,构造a数列使得 W[a[1]] < W[a[2]] < ... < W[a[n]] AND S[a[1]] > S[a[2]] > ... > S[a[n]]成立 思路:类似求一个序列的最长上升子序列一样的求法,需要保存路径,dp[i]表示以第i个数为结尾能构造出次数列的最长长度,pre[i]保存路径。#include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; #define N 1004 struct M{ int w, s, id; bool operator<(const M a)const { if (w == a.w) return s > a.s; return w < a.w; } }a[N]; int dp[N], pre[N]; vector<int>A; int main() { int i, j, n = 1; while (~scanf("%d%d", &a[n].w, &a[n].s)) { a[n].id = n, n++; } sort(a, a+n); memset(pre, -1, sizeof(pre)); memset(dp, 0, sizeof(dp)); a[0].w = 0, a[0].s = 10001, a[0].id = 0; for (i = 1;i < n;i++) { int tm = 0, td = 0; for (j = 0;j < i;j++) { if (a[i].w > a[j].w && a[i].s < a[j].s) { if (dp[j] > tm) tm = dp[j], td = j; } } dp[i] = tm+1, pre[i] = td; } int ans = 0, td = 0; for (i = 1;i < n;i++) { if (ans < dp[i]) ans = dp[i], td = i; } A.clear(); while (td != 0 && td != -1) { A.push_back(a[td].id), td = pre[td]; } printf("%d\n", ans); for (i = 0;i < A.size();i++) { printf("%d\n", A[A.size()-1-i]); } }