zoukankan      html  css  js  c++  java
  • The Twin Towers zoj2059 DP

    The Twin Towers

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Twin towers we see you standing tall, though a building's lost our faith will never fall.
    Twin towers the world hears your call, though you're gone it only strengthens our resolve.
    We couldn't make it through this without you Lord, through hard times we come together more. ... 

    Twin Towers - A Song for America

    In memory of the tragic events that unfolded on the morning of September 11, 2001, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has collected for years. Will she succeed in building the two towers of the same height?


    Input

    There are mutiple test cases.

    One line forms a test case. The first integer N (N < 100) tells you the number of crystals her brother has collected. Then each of the next N integers describs the height of a certain crystal.

    A negtive N indicats the end.

    Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.


    Output

    If it is impossible, you would say "Sorry", otherwise tell her the height of the Twin Towers.


    Sample Input

    4 11 11 11 11
    4 1 11 111 1111 
    -1


    Sample Output

    22
    Sorry

     1 #include <iostream>
     2 #include <math.h>
     3 #include <stdio.h>
     4 #include <string.h>
     5 using namespace std;
     6 #define mod 1000000000
     7 int a[101],b[101][1001];
     8 int ma(int x,int y)
     9 {
    10     return x>y?x:y;
    11 }
    12 int main()
    13 {
    14     int n,i,j;
    15     while(scanf("%d",&n))
    16     {
    17         if(n<0)break;
    18         int s=0;
    19         for(i=1; i<=n; i++)scanf("%d",&a[i]),s+=a[i];
    20         s/=2;
    21         memset(b,0,sizeof(b));
    22         for(i=1; i<=n; i++)
    23         {
    24             for(j=0; j<=s; j++)
    25             {
    26                 if(b[i-1][j])
    27                 {
    28                     b[i][j+a[i]]=ma(b[i][j+a[i]],b[i-1][j]+a[i]);
    29                     b[i][j-a[i]>0?j-a[i]:a[i]-j]=ma(b[i][j-a[i]>0?j-a[i]:a[i]-j],ma(b[i-1][j],b[i-1][j]-j+a[i]));
    30                 }
    31             }
    32             b[i][a[i]]=ma(a[i],b[i][a[i]]);
    33             for(j=0; j<=s; j++)
    34                 b[i][j]=ma(b[i-1][j],b[i][j]);
    35         }
    36         if(b[n][0])
    37             printf("%d
    ",b[n][0]);
    38         else puts("Sorry");
    39     }
    40 }
    View Code
  • 相关阅读:
    字符的编码
    数据的基本类型和内置方法(二)
    基本的数据类型和内置方法介绍 (一)
    流程运算 if while for
    用户交换 基本数据类型 基本运算符 格式化输出
    机器语言发展简介和变量的介绍
    计算机基础
    Python学习建议和要求总结
    CH135 最大子序和 题解报告
    HRBUST1356 Leyni,罗莉和队列 题解报告
  • 原文地址:https://www.cnblogs.com/ERKE/p/3578956.html
Copyright © 2011-2022 走看看