Because it is a partial-order relationship, it is obvious that DP will get the result by first sorting and then DP.
1. Pay attention to the idea of DP
2. Note the sort here. From low to high according to the most easily meet the conditions and the most demanding start row
3. Using int directly to calculate the area will overflow, either set to long long or not directly calculate the area
Code:
#include <iostream> #include <algorithm> #include <cmath>using namespace Std;int n;long long ans,dp[ 1009];struct h{int len,wid,thk,d;} Code[1009];bool cmp (H a,h B) {if (A.len!=b.len) return a.len<b.len; if (A.WID!=B.WID) return a.wid<b.wid; return A.D>B.D;} void DP () {ans=-2;for (int i=1;i<=n;i++) {if (code[i].d==0) for (int j=i-1;j>0;j--) {if (Code[j].len<=code[i]. len&&code[j].wid<=code[i].wid&& (DP[J]+CODE[I].THK) >dp[i]) DP[I]=DP[J]+CODE[I].THK;} else if (code[i].d==1) for (int j=i-1;j>0;j--) {if (Code[j].len<=code[i].len&&code[j].wid<=code[i]. wid&& (code[j].len<code[i].len| | Code[j].wid<code[i].wid) && (DP[J]+CODE[I].THK) >dp[i]) DP[I]=DP[J]+CODE[I].THK;} elsefor (int j=i-1;j>0;j--) {if (code[j].len<code[i].len&&code[j].wid<code[i].wid&& (Dp[j] +CODE[I].THK) >dp[i]) DP[I]=DP[J]+CODE[I].THK;} if (Ans<dp[i]) ans=dp[i];}} int main () {while (cin>>n) {if (!n) break;for (int i=1;i<=n;i++) {CIn>>code[i].len>>code[i].wid>>code[i].thk>>code[i].d;if (Code[i].len<code[i].wid) Swap (CODE[I].LEN,CODE[I].WID);} Sort (code+1,code+n+1,cmp); for (int i=1;i<=n;i++) DP[I]=CODE[I].THK;DP (); Cout<<ans<<endl;}}
HDU 4001--dp--(First Dp)