ZOJ 2059 The Twin Towers(双塔DP)
2016-05-06 22:43
323 查看
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
双塔DP
dp[i][2000] 表示第i个砖块,堆成两个塔的高度差,每个砖块有两个选择要么不用,要么放左边的塔,要么放右边的塔
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
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
双塔DP
dp[i][2000] 表示第i个砖块,堆成两个塔的高度差,每个砖块有两个选择要么不用,要么放左边的塔,要么放右边的塔
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> #include <stdio.h> using namespace std; int a[105]; int dp[105][4005]; int n; int main() { while(scanf("%d",&n)!=EOF) { if(n<0) break; for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(dp,-1,sizeof(dp)); dp[0][0+2000]=0; for(int i=1;i<=n;i++) { memcpy(dp[i],dp[i-1],sizeof(dp[i])); for(int j=-1999;j<=1999;j++) { if(dp[i-1][j+2000]==-1) continue; if(j<0) { dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+a[i]); dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+max(0,j+a[i])); } else { dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+a[i]); dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+max(0,a[i]-j)); } } } if(dp [2000]!=0&&dp [2000]!=-1) printf("%d\n",dp [2000]); else printf("Sorry\n"); } return 0; }
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
相关文章推荐
- 网络请求遇到的一个问题
- ZOJ 2059 The Twin Towers(双塔DP)
- 【机器学习入门——2】Pycharm 安装 autopep8
- ecshop系统中的Ajax使用事例方法
- 第10周项目一-点-圆-圆柱类的设计
- SVN案例
- runtime获取一个控件的所有属性
- 霍夫变换(Hough)
- JAVA集合
- PHP 路由配置
- 大数据在高校中的应用
- 打字第四天~
- SparkStreaming 事务处理彻底掌握
- Linux 暂时和永久性修改DNS
- 5-项目进度管理和项目成本管理
- 整理 java 分页思路
- linux命令(37):locate命令
- C++ STL vector容器学习
- 设计模式之状态模式
- Servlet——实现数据分页显示