Shu_1299 vijos 1037 (double tower construction)

Source: Internet
Author: User

Http: // 202.121.199.212/judgeonline/problem. php? Cid = 1078 & pid = 7


Analysis: typical DP questions

DP [I] [J]: The height of the tall tower in the double tower where the height difference of the first crystal is J;

DP [I] [J] = d [I-1] [J], do not put the I crystal;

DP [I] [J] = f [I-1] [H [I]-J] + J, the I crystal is placed on a shorter tower, it turns a shorter tower into a higher one;

DP [I] [J] = f [I-1] [H [I] + J], the I crystal is placed on the shorter Tower, the shorter tower is still a shorter tower;

DP [I] [J] = f [I-1] [J-H [I] + H [I], the I crystal is placed on a high tower, the original high tower becomes a higher tower;

(You can draw a graph and the transfer equation will come out)


Code:

#include <iostream>#include <stdio.h>#include <string>#include <string.h>using namespace std; int h[120];int dp[120][2400]; int mmax(int a,int b){    return a<b ? b:a;} int main(){    //freopen("in.txt","r",stdin);     int n;    int sum=0;    scanf("%d",&n);    memset(dp, -1, sizeof(dp));    dp[0][0]=0;     for(int i=1;i<=n;i++) {scanf("%d",&h[i]); sum +=h[i];}     for(int i = 1; i <=n; i++) {        for(int j = 0; j <= sum; j++) {            if(dp[i-1][j]>-1)               dp[i][j]=dp[i-1][j];            if(h[i]>j && dp[i-1][h[i]-j]>-1)               dp[i][j]=mmax(dp[i][j],dp[i-1][h[i]-j]+j);            if(j+h[i]<=sum && dp[i-1][j+h[i]]>-1)               dp[i][j]=mmax(dp[i][j],dp[i-1][j+h[i]]);            if(j>=h[i] && dp[i-1][j-h[i]]>-1)               dp[i][j]=mmax(dp[i][j],dp[i-1][j-h[i]]+h[i]);        }    }    if(dp[n][0] > 0)   printf("%d\n",dp[n][0]);    else               printf("Impossible\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.