zoj 2059 The Twin Towers(DP)
2014-03-03 23:11
344 查看
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
题意:给出n个小塔,每个小塔都有一个高度,问能否利用这些小塔组成两个高度相等的塔(不一定要所有小塔都用完),若能,输出最大的高度。
思路:DP,背包思想。题目说了组成的塔高度小于2000,设dp[i][j]表示搜索到第i个高度、两塔高度差为j时,较高的塔的最大高度。
转移方程见代码:
AC代码:
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
题意:给出n个小塔,每个小塔都有一个高度,问能否利用这些小塔组成两个高度相等的塔(不一定要所有小塔都用完),若能,输出最大的高度。
思路:DP,背包思想。题目说了组成的塔高度小于2000,设dp[i][j]表示搜索到第i个高度、两塔高度差为j时,较高的塔的最大高度。
转移方程见代码:
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int INF = 2000000000; const int maxn = 20005; const int mod = 1e9; int n; int dp[105][2005], a[105]; //dp[i][j]表示搜索到第i个高度、两塔高度差为j时,较高的塔的最大高度 int main() { while(scanf("%d", &n)) { if(n < 0) break; int sum = 0; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; } memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; i++) { for(int j = 0; j <= sum; j++) { if(dp[i - 1][j]) { //加到较高的塔上 if(j + a[i] <= sum) dp[i][j + a[i]] = max(dp[i][j + a[i]], dp[i - 1][j] + a[i]); //加到较低的塔上,较低的塔仍比高的低 if(j >= a[i]) dp[i][j - a[i]] = max(dp[i][j - a[i]], dp[i - 1][j]); //加到较低的塔上,较低的塔变成较高的塔 else dp[i][a[i] - j] = max(dp[i][a[i] - j], dp[i - 1][j] + a[i] - j); } } dp[i][a[i]] = max(dp[i][a[i]], a[i]); for(int j = 0; j <= sum; j++) dp[i][j] = max(dp[i][j], dp[i - 1][j]); //可不取当前这个高度 } if(dp [0]) printf("%d\n", dp [0]); else printf("Sorry\n"); } return 0; }
相关文章推荐
- ZOJ 2059 The Twin Towers(双塔DP)
- ZOJ 2059 The Twin Towers(dp)
- ZOJ 2059 The Twin Towers(双塔DP)
- zoj2059 The Twin Towers (dp)
- ZOJ2059 The Twin Towers,DP
- UVA 10066 The Twin Towers 双塔 LCS,dp
- ZOJ 2059 The Twin Towers
- zoj2059 The Twin Towers(经典的DP)
- 【解题报告】uva10066_The Twin Towers(双塔, dp, LCS)
- UVA.10066 The Twin Towers (DP LCS)
- uva10066 - The Twin Towers(dp,lcs)
- zoj 2059 - The Twin Towers
- uva10066 The Twin Towers(dp 最长公共子序列)
- zoj 2059 The Twin Towers
- ZOJ 3329 One Person Game 概率dp 处理环
- zoj 3537 Cake 【凸包 + 区间dp】 【最优三角剖分】
- zoj-3626 Treasure Hunt I (树形dp)
- zoj-3626 Treasure Hunt I (树形dp)
- ZOJ - 3777 Problem Arrangement(状态压缩dp)
- ZOJ 3471 Most Powerful(状压DP)