#bzoj1506#双塔问题(Dp经典)
来源:互联网 发布:单片机流水灯不亮 编辑:程序博客网 时间:2024/06/14 08:20
1506: 双塔问题
时间限制:1 Sec 内存限制: 64 MB题目描述
第三题:双塔问题 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难。为了纪念"9?11"事件,Mr. F决定自己用水晶来搭建一座双塔。 Mr. F有N块水晶,每块水晶有一个高度,他想用这N块水晶搭建两座有同样高度的塔,使他们成为一座双塔,Mr. F可以从这N块水晶中任取M(1≤M≤N)块来搭建。但是他不知道能否使两座塔有同样的高度,也不知道如果能搭建成一座双塔,这座双塔的最大高度是多少。所以他来请你帮忙。 给定水晶的数量N(1≤N≤100)和每块水晶的高度Hi(N块水晶高度的总和不超过2000),你的任务是判断Mr. F能否用这些水晶搭建成一座双塔(两座塔有同样的高度),如果能,则输出所能搭建的双塔的最大高度,否则输出"Impossible"。
输入
输入的第一行为一个数N,表示水晶的数量。第二行为N个数,第i个数表示第i个水晶的高度。
输出
输出仅包含一行,如果能搭成一座双塔,则输出双塔的最大高度,否则输出一个字符串"Impossible"。
样例输入
51 3 4 5 2
样例输出
7
定义
Dp[i][j]表示前i个水晶,搭建成高度差为j的双塔的共同最高高度
Dp[i][j] = Dp[i - 1][j],Dp[i - 1][j + H[i]], Dp[i - 1][j - H[i]] + H[i], Dp[i - 1][H[i] - j] + j
Code:
/*For the i block, we firstly have two choices, to put i block or not,if not, we have f[i][j] = f[i - 1][j];if so, we secondely have two choices, to put it onto the higher one or the shorter one.the higher one:of course the dissmilarity will change into a bigger one.we have f[i][j] = f[i - 1][j + H[i]]the shorter one:we will have two situation for the dissmilarity.H[i] > jtwo towers' positions of heights will be exchanged,and the f will be added jwe have f[i][j] = f[i - 1][H[i] - j] + jH[i] <= jtwo towers' positions of heights remain,adn the f will added H[i]we have f[i][j] = f[i - 1][j - H[i]] + H[i]f[i][j]:i refers to there has already been i blocks which are used.j refers to the dissmilarity between the higher one and the shorter one.f refers to the height the two towers have.f[i][j] = f[i - 1][j]not put i inf[i - 1][j + H[i]]onto the higherf[i - 1][H[i] - j] + jpositions exchangedf[i - 1][j - H[i]] + H[i]remain*/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;int H[110], f[110][2010];int main(){ int N; scanf("%d", &N); for(int i = 1; i <= N; i++) scanf("%d", &H[i]); memset(f[0], -0x3f, sizeof(f[0])); f[0][0] = 0; for(int i = 1; i <= N; i++) for(int j = 0; j <= 2000; j++){ f[i][j] = max(f[i - 1][j], f[i - 1][j + H[i]]); if(j >= H[i]) f[i][j] = max(f[i][j], f[i - 1][j - H[i]] + H[i]); else f[i][j] = max(f[i][j], f[i - 1][H[i] - j] + j); } if(f[N][0]) printf("%d\n", f[N][0]); else printf("Impossible\n"); return 0;}
阅读全文
0 0
- #bzoj1506#双塔问题(Dp经典)
- dp 数塔 经典动态规划问题
- 邮局--dp经典问题
- 【DP经典问题】WordBreak
- 树形DP经典问题
- 经典DP问题
- 【DP经典问题】
- 硬币问题 经典dp
- dp经典问题
- 硬币问题(经典dp)
- poj1157(花店问题+经典DP)
- 经典DP题目-滑雪问题
- POJ1157 花店问题经典DP
- hdu 2224 经典DP 双调旅行商问题
- 环形石子合并问题 - 经典DP问题
- ZOJ1733 POJ1458 Common Subsequence,经典DP问题
- hoj 整数划分问题 经典dp
- 入门经典 DP 0-1背包问题
- 生活中的某一天
- Uva-1626 lrj-P278 区间dp
- 有内存限制的海量数据排序
- 华为笔试:删除字符串中出现次数最少的字符
- Android内存优化汇总
- #bzoj1506#双塔问题(Dp经典)
- C++学习15:指针与const
- vue环境搭建与创建第一个vuejs文件
- java初始化与清理
- 谈谈style属性
- ckedittor在线编辑器的用法
- 深入理解JAVA虚拟机读书笔记----垃圾收集器与内存分配策略
- 搭建双网络Oracle 12C R1 RAC+ASM
- Node.js第一天学习总结和第一个小例子