ZOJ 2059 The Twin Towers(dp)
2015-09-22 12:25
363 查看
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 Author: HU, Shichao Source: ZOJ Monthly, December 2003 Submit Status |
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-12 typedef long long ll; using namespace std; #define N 3005 int dp[105] ; int n,a ; void DP() { int i,j; memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int i=0;i<n;i++) for(int v=0;v<2005;v++) { dp[i+1][v]=max(dp[i+1][v],dp[i][v]); if(dp[i][v]<0) continue; if(v+a[i+1]<2005) dp[i+1][v+a[i+1]]=max(dp[i+1][v+a[i+1]],dp[i][v]+a[i+1]); if(a[i+1]<=v) dp[i+1][v-a[i+1]]=max(dp[i+1][v-a[i+1]],dp[i][v]); if(a[i+1]>=v) dp[i+1][a[i+1]-v]=max(dp[i+1][a[i+1]-v],dp[i][v]+a[i+1]-v); } } int main() { int i,j; while(scanf("%d",&n)&&n>0) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); DP(); if(dp [0]>0) printf("%d\n",dp [0]); else printf("Sorry\n"); } return 0; } /* 4 11 11 11 11 4 1 11 111 1111 -1 */
相关文章推荐
- SQL Server 2008 允许远程连接的配置
- PS打造流行漂亮逼真的凹版烫金效果
- 分享ShakaApktool & apktool使用方法
- 自定义控件(视图)28期笔记02:自定义控件之使用系统控件(优酷案例之环形菜单布局)
- Linux知识(1)----U盘安装Ubantu14.04系统
- VC调用FFMPEG release版出错的解决办法
- 括号匹配
- spring websocket自动断开连接再创建引发的问题解决方案
- c语言学习笔记(3)const和volatile以及const volatile int i=0分析
- IOS-UI-基础开发
- 【Bootstrap】兼容IE8、谷歌和其他主流浏览器的观众IMAX风格的页面
- 关于nRF24L01地址问题
- winform实现拖动文件到窗体上的方法
- onActivityResult无故被调用
- 小白进行时,char* 转 CString
- 潘鹏整理WPF(8)列表控件ListBox&&ComboBox
- 浮点型计算如何确定精度
- linux常用命令(44):netstat 命令
- java于23设计模式
- more命令