9.2.2 (rectangular nesting _ Simple Dynamic Planning)

Source: Internet
Author: User

Dizzy, and finally found out where I was wrong ,,,

The DP Method Used by others' standard processes is a memory-based search ,,
Although your memory is there, you did not make good use of the results at one time,

That is, if you do not have one DP, you can save the result,

Check the Code:

# Include <stdio. h> # include <string. h> # include <iostream> # include <string> # define max (a, B) (a)> (B )? (A): (B) using namespace STD; struct node {int A; int B;} Q [1005]; int N; int map [1005] [1005]; int d [1005]; bool judge (int I, Int J) {If (Q [I]. A <q [J]. A & Q [I]. B <q [J]. B | Q [I]. A <q [J]. B & Q [I]. B <q [J]. a) {return true;} return false;} int dp (int I) {// Of course, the ANS here can also be expressed directly using D [I, however, if a multi-dimensional array appears, ANS will show its superiority in Int & Ans = d [I]; // and this is the essence of this question, it is the gap between your TLE and your no TLE. If you don't have this sentence, it will be a lot more. If (ANS> 0), then it will be the essence of the memory-based search, is to see if d [I] has been obtained; if it is obtained, it does not need to be calculated, {return ans;} ans = 1; for (Int J = 0; j <N; j ++) {If (Map [J] [I]) {ans = max (ANS, dp (j) + 1) ;}return ans ;} int main () {int t; scanf ("% d", & T); While (t --) {memset (MAP, 0, sizeof (MAP); scanf ("% d ", & N); // If (n = 0) // {// printf ("-1 \ n"); // continue; //} For (INT I = 0; I <n; I ++) {scanf ("% d", & Q [I]. a, & Q [I]. b) ;}for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {If (Judge (I, j) {// cout <"I =" <I <"" <"J =" <j <Endl; map [I] [J] = 1 ;}} int T = 0; memset (D, 0, sizeof (d); For (INT I = 0; I <n; I ++) {d [I] = dp (I); // printf ("DP [% DS] = % d", I + 1, d [I]); // cout <Endl; t = max (T, d [I]);} printf ("% d \ n", T );} // system ("pause"); Return 0 ;}

Modify the values and output them in Lexicographic Order:

The number of your rectangle:

# Include <stdio. h> # include <string. h> # include <iostream> # include <string> # define max (a, B) (a)> (B )? (A): (B) using namespace STD; struct node {int A; int B;} Q [1005]; int N; int CNT; int map [1005] [1005]; int d [1005]; int seq [1005]; bool judge (int I, Int J) {If (Q [I]. A <q [J]. A & Q [I]. B <q [J]. B | Q [I]. A <q [J]. B & Q [I]. B <q [J]. a) {return true;} return false;} void print_ans (int I) {// printf ("% d", I + 1); For (Int J = 0; j <n; j ++) {If (Map [J] [I] = 1 & D [I] = d [J] + 1) {seq [CNT ++] = J + 1; print_ans (j); break ;}} int dp (int I) {// Of course, the ANS here can also be expressed directly using D [I], but if there is a multi-dimensional array ,,, ans shows his superiority. Int & Ans = d [I]; // This is the essence of this question, that is, the gap between you and your TLE, if you do not have this sentence, you will get a lot more. If (ANS> 0) then, if (ANS> 0) // This is the essence of the memory-based search, is to see if d [I] has been obtained; if it is obtained, it does not need to be calculated, {return ans;} ans = 1; for (Int J = 0; j <N; j ++) {If (Map [J] [I]) {ans = max (ANS, dp (j) + 1) ;}return ans ;} int main () {int t; scanf ("% d", & T); While (t --) {memset (MAP, 0, sizeof (MAP); scanf ("% d ", & N); // If (n = 0) // {// printf ("-1 \ n"); // continue; //} For (INT I = 0; I <n; I ++) {scanf ("% d", & Q [I]. a, & Q [I]. b) ;}for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {If (Judge (I, j) {// cout <"I =" <I <"" <"J =" <j <Endl; map [I] [J] = 1 ;}} int T = 0; int flag; memset (D, 0, sizeof (d); int I; for (I = 0; I <n; I ++) {d [I] = dp (I); // printf ("DP [% DS] = % d ", I + 1, D [I]); // cout <Endl; // T = max (T, d [I]); If (T <D [I]) {T = d [I]; flag = I ;}} CNT = 0; seq [CNT ++] = Flag + 1; print_ans (FLAG ); for (Int J = CNT-1; j> = 0; j --) {printf ("% d", seq [J]);} printf ("\ n "); printf ("% d \ n", T);} system ("pause"); 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.