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
  • 相关阅读:
    软件测试培训第9天
    软件培训第8天
    软件测试培训第7天
    软件测试培训第5天
    软件测试培训第6天
    软件测试培训第4天
    软件测试培训第3天
    MySQL复杂用法
    MySQL的基本语法
    VM虚拟机上安装Redhat
  • 原文地址:https://www.cnblogs.com/ERKE/p/3578956.html
Copyright © 2011-2022 走看看