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)