题目

Source

http://www.lightoj.com/volume_showproblem.php?problem=1126

Description

Professor Sofdor Ali is fascinated about twin towers. So, in this problem you are working as his assistant, and you have to help him making a large twin towers.

For this reason he gave you some rectangular bricks. You can pick some of the bricks and can put bricks on top of each other to build a tower. As the name says, you want to make two towers that have equal heights. And of course the height of the towers should be positive.

For example, suppose there are three bricks of heights 3, 4 and 7. So you can build two towers of height 7. One contains a single brick of height 7, and the other contains a brick of height 3 and a brick of height 4. If you are given bricks of heights 2, 2, 3 and 4 then you can make two towers with height 4 (just don't use brick with height 3).

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with an integer n (1 ≤ n ≤ 50), denoting the number of bricks. The next line contains n space separated integers, each containing the height of the brick hi (1 ≤ hi ≤ 500000). You can safely assume that the sum of heights of all bricks will not be greater than 500000.

Output

For each case, print the case number and the height of the tallest twin towers that can be built. If it's impossible to build the twin towers as stated, print "impossible".

Sample Input

4
3
3 4 7
3
10 9 2
2
21 21
9
15 15 14 24 14 3 20 23 15

Sample Output

Case 1: 7
Case 2: impossible
Case 3: 21
Case 4: 64

分析

题目大概说有n个各有高度的砖头,要用它们叠出两个高度一样的塔,问高度最多为多少。

  • dp[i][j]表示前i个砖头中两塔高度差为j能叠出的最高的那座塔的高度
  • dp[n][0]即为所求
  • 对于第i个砖头不取、取且叠在低塔上、取且叠在高塔上,从i-1的状态转移
  • 另外,空间比较大可以用滚动数组

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[2][500100];
int main(){
int t,n,a;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d",&n);
memset(d,-1,sizeof(d));
d[0][0]=0;
int k=0;
for(int i=0; i<n; ++i){
scanf("%d",&a);
memcpy(d[!k],d[k],sizeof(d[k]));
for(int j=0; j<=500000; ++j){
if(d[k][j]==-1) continue;
if(j+a<=500000) d[!k][j+a]=max(d[!k][j+a],d[k][j]+a);
if(a>j){
d[!k][a-j]=max(d[!k][a-j],d[k][j]+a-j);
}else{
d[!k][j-a]=max(d[!k][j-a],d[k][j]);
}
}
k^=1;
}
if(d[k][0]==0) printf("Case %d: impossible\n",cse);
else printf("Case %d: %d\n",cse,d[k][0]);
}
return 0;
}

LightOJ1126 Building Twin Towers(DP)的更多相关文章

  1. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

  2. UVA 10066 The Twin Towers(LCS)

    Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...

  3. 【BZOJ】2021: [Usaco2010 Jan]Cheese Towers(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2021 噗,自己太弱想不到. 原来是2次背包. 由于只要有一个大于k的高度的,而且这个必须放在最顶,那 ...

  4. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  5. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  6. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  7. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  8. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  9. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

随机推荐

  1. 树莓派笔记之使用netselect选择最快Raspbian软件源

    背景: 之前在葉難大大的部落格里看到有讲可以使用netselect查找最快软件源,今天正好看到, 特此记下来,因为之前一直使用中国科学技术大学的源,结果发现不是我这里最快的. 注意: 以下仅对Rasp ...

  2. awk 的使用方法

    awk 的使用方法 我们知道 awk 的常用变量包括: $0 当前记录(这个变量中存放着整个行的内容) $1~$n 当前记录的第 n个字段,字段间由 FS 分隔 NF 当前记录中的字段个数,就是有多少 ...

  3. 即时搜索或input实时检测监听输入框变化

    js实现的文本框内容发生改变立马触发事件简单介绍:本章节介绍一下如何在文本框的内容发生变化的时候,立马触发一个事件执行响应的操作,而不是像是keydow或者keyup事件一样,只能够检测通过键盘输入导 ...

  4. 5、Servlet的使用

    一.什么是Servlet:用于开发动态Web资源的的技术.使用Servlet可以读取来自用户端的数据,而实现了用户与服务器之间的动态数据交互.更简单的说就是连接页面和代码. 1.开发一个动态的Web资 ...

  5. JavaScript URL编码转换函数 encodeURIComponent()

    encodeURIComponent()定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 语法:encodeURIComponent(URIstring ...

  6. nginx自动检测后台服务器健康状态

    转自http://www.iyunv.com/thread-38535-1-1.html 公司业务线上对后端节点的健康检查是通过nginx_upstream_check_module模块做的,这里我将 ...

  7. 我常用的Vi命令

    Vi对于linux的重要性和受欢迎的程度在此一律不表.此刻互联网上不少介绍vi的文章和博客,相信写得比我好的也不在少数.然而为什么我依然写这样一篇文章呢?我对linux知识和了解也都来自于互联网,很难 ...

  8. iOS 归档archive使用方法

    归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存,文件将被保存成自定 义类型的文件,相对于NSUserDefault具有更好的保密性.   1.使用archiveRootObject ...

  9. 红黑树/B+树/AVL树

    RB Tree 红黑树  :http://blog.csdn.net/very_2/article/details/5722682 Nginx的RBTree实现   :http://blog.csdn ...

  10. 前端 js 实现简单 表单提交

    1. 登录页 验证用户身份,登录成功之后等待一定秒数,跳转到操作页面 <html> <head> <title>Login.html</title> & ...