UVA 10131-is Bigger Smarter

Source: Internet
Author: User

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&GT;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&LT;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", &AMP;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); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.