The main idea: give some elephants, including its weight, IQ. To find the longest sequence, the higher the weight, the lower the IQ. (Strictly increase or decrease)
Topic Type: Dp/lis
Topic Analysis:
Sort the elephants in ascending order of weight, and then find the longest single minus subsequence in this sequence for IQ attributes. It should be noted that the subject requirements are strictly increase or decrease, so in the judging conditions to consider the same situation to exclude (mainly weight).
Also note that after sorting, the order is not the original order, and the topic requires the output of the original sequence. So maintain a r[] array when sorting.
For DP, state transfer equation (2 types) for the longest single-decrement sequence:
D[i] = max{d[j]+1 | s[i]>s[j]; j = (i, n);}//d[i] represents the longest descending subsequence starting with I
D[i] = max{d[j]+1 | s[i]<s[j]; j = [0, i);}//d[i] indicates the longest descending subsequence ending with I
Code:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 1002 struct Ele {int w, s; int next; int xh;} E[MAXN]; int R[MAXN]; Used to retain the corresponding original sequence int n; int D[MAXN]; int VIS[MAXN]; D[i] = max{d[j]+1 | s[i]>s[j]; j = (i, n);}//d[i] represents the longest descending subsequence starting with I//d[i] = max{d[j]+1 | s[i]<s[j]; j = [0, i); }//d[i] represents the longest descending subsequence that ends with I int dp (int cur)//with Cur, doing the opening {if (Vis[cur]) return d[cur]; vis[cur] = 1; int max = 1; for (int i= cur+1; i<n; i++) if (e[i].s<e[cur].s && E[I].W>E[CUR].W)//Find Descending//Note w strictly increment ②{int t = DP (i) +1; max = max>t? Max: (e[cur ].next = i, t); } return D[cur] = max; } void print (int x) {//d[x] = 1 Description Stop while (1) {printf ("%d/n", e[x].xh+1);//starting from 1, ④///////////Debug//printf ("w=%d, I q=%d/n ", E[X].W, E[X].S); if (d[x]==1) break; x = E[x].next; }} int cmp (Ele A, ele b)//sort parameters can be used instead of const to change the value, very useful. ③{if (A.W<B.W) {return 1;} else {int t = a.xh; a.xh = b.xh; b.xh = t; return 0;}} int main () {n=0; for (int i=0; scanf ("%d%d", &E[I].W, &e[i].s)!=eof; i++, n++) {e[i].xh = i;} sort (E, e+n, CMP); Press W Ascending memset (Vis, 0, sizeof (VIS)); int max = 1; int ans; for (int i=0; i<n; i++) {max = max > dp (i)? Max: (ans = i, DP (i));} printf ("%d/n", Max); print (ANS); }