Question: What is a bigger smarter? (DAG)
The weight and IQ of a group of elephants are given. The largest number of elephants is required to form a sequence of strictly increasing weights and decreasing IQ. Output the largest number of elephants and the sequence of these elephants (one of them is acceptable ).
Solution: DP on Dag. Similar to the previous article. Uva437-The Tower of Babylon (DP on DAG ). It is to form a directed edge when every two elephants meet the above sequence requirements.
And then the DP on the Dag. And then output the path.
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) continue; 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 ); break ;}} int main () {n = 0; while (scanf ("% 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); printf_ans (x, y);} else printf ("1 \ N1 \ n "); return 0 ;}