What is a bigger smarter? (DP on DAG)

Source: Internet
Author: User

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 ;}



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.