HDU 4001--dp--(First Dp)

Source: Internet
Author: User

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&GT;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)

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.