Uva-10131is bigger Smarter? (DP on DAG)

Source: Internet
Author: User

Title: Uva-10131is bigger Smarter?

(DAG)


Give the weight and IQ of a group of elephants. Requires the most selected elephants to form a sequence. Strict weight increment, IQ descending sequence. The number of elephants that output the most and the sequence of these elephants (one of them is possible).


Problem solving: DP on a DAG. Similar to a previous article. Uva437-the Tower of Babylon (DP on DAG). is to have each of the two elephants meet the sequence requirements above the formation of a forward edge.

This is followed by the DP on the DAG. Then the path is output.


Code:

#include <cstdio> #include <cstring>const int N = 1005;int elephants[n][2];int d[n][n];int g[n][n];int n;void Init () {memset (d,-1, sizeof (d));} void handle () {memset (g, 0, sizeof (g)), for (int i = 0; i < n; i++) for (int j = 0; J < N; j + +) {if (i = = j) Continu E;if (Elephants[i][0] < elephants[j][0] && elephants[i][1] > Elephants[j][1]) g[i][j] = 1;}} int Max (const int A, const int b) {return a > B? a:b;} int DP (int x, int y) {int& ans = d[x][y];if (ans! =-1) return ans;for (int i = 0; i < n; i++) {if (i = = y) continue if (G[y][i]) ans = Max (ans, DP (y, i) + 1);} if (ans = =-1) ans = 2;return ans;} void Printf_ans (int x, int y) {printf ("%d\n", X + 1), if (d[x][y] = = 2) {printf ("%d\n", y + 1); return;} for (int i = 0; i < n; i++) {if (i = = y) continue;if (G[y][i] && d[x][y] = D[y][i] + 1) {Printf_ans (y, i); Brea k;}}} int main () {n = 0;while (scanf ("%d%d", &elephants[n][0], &elephants[n][1])! = EOF) {n++;} Handle (); init (); int ans, Temp;ans = -1;int x, y;for (int i = 0; i < n; i++) for (int j = 0; J < N; j + +) {if (G[i][j]) {temp = DP (i, j); if ( Temp > ans) {x = I;y = j;} Ans = Max (ans, temp);}} if (ans! =-1) {printf ("%d\n", ans);p Rintf_ans (x, y),} else printf ("1\n1\n"); return 0;}



Uva-10131is bigger Smarter? (DP on DAG)

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.