zoj2059 The Twin Towers (dp)
2015-08-26 20:38
337 查看
Description
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[a][b] 第a个石头,,两塔的高度差。。不断地去维护低塔。。
后来又发现了可以再优化。。。。。
用滚动数组优化内存(虽然并没有什么卵用)(给自己提个醒,有这个用法吧)
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[a][b] 第a个石头,,两塔的高度差。。不断地去维护低塔。。
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<stack> #pragma comment(linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define EPS 1e-6 #define INF (1<<24) using namespace std; int dp[105][2005]; int a[105]; int main() { int n; while(scanf("%d",&n),n>0) { int i,j; for(i=1;i<=n;i++) scanf("%d",&a[i]); memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(i=1;i<=n;i++) { memcpy(dp[i],dp[i-1],sizeof(dp[i-1]));//不放i,i-1的情况全是i的情况 for(j=0;j<2001;j++) //高度 { if(dp[i-1][j]!=-1) { if(a[i]<j) { dp[i][j-a[i]]=max(dp[i][j-a[i]],dp[i-1][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]+j); //放到低塔上 dp[i][j+a[i]]=max(dp[i][j+a[i]],dp[i-1][j]);//放到高塔上 } } } } if(dp [0]) printf("%d\n",dp [0]); else printf("Sorry\n"); } return 0; }
后来又发现了可以再优化。。。。。
用滚动数组优化内存(虽然并没有什么卵用)(给自己提个醒,有这个用法吧)
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> #include<stack> #pragma comment(linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define EPS 1e-6 #define INF (1<<24) using namespace std; int dp[2005]; //dp[j]==高度相差j的矮塔的高度 int t[2005]; //滚动数组,记录相当于dp[i] int a[105]; int main() { int n; while(scanf("%d",&n),n>0) { int i,j; for(i=1;i<=n;i++) scanf("%d",&a[i]); memset(dp,-1,sizeof(dp)); memset(t,-1,sizeof(t)); dp[0]=t[0]=0; for(i=1;i<=n;i++) { memcpy(dp,t,sizeof(t));//不放i,i-1的情况全是i的情况 for(j=0;j<2001;j++) { if(dp[j]!=-1) { if(a[i]<j) { t[j-a[i]]=max(t[j-a[i]],dp[j]+a[i]);//放到低塔上 t[j+a[i]]=max(t[j+a[i]],dp[j]);//放到高塔上 } else { t[a[i]-j]=max(t[a[i]-j],dp[j]+j); //放到低塔上 t[j+a[i]]=max(t[j+a[i]],dp[j]);//放到高塔上 } } } memcpy(dp,t,sizeof(t));//滚动数组,传递数据 } if(dp[0]) printf("%d\n",dp[0]); else printf("Sorry\n"); } return 0; }
相关文章推荐
- HDU 5375(Gray code-格雷码dp)
- 插入排序(java实现)
- iOS学习 - 汤姆猫
- SQLite学习笔记(12)-事务(1)
- 9+实现斐波那契Fibonacci数列f(n),
- 属性动画实现viewpager改变页面的时候,带有指示条的动画效果
- 按键中断
- C代码中如何调用C++ C++中如何调用C
- LeetCode题解:Rotate Array
- Proud Merchants(01背包)
- TCP/IP详解 卷I:协议 のUDP 用户数据包协议
- Java IO
- 计算机网络面试题2
- caffe优化算法
- 自定义视图.
- 嵌入式linux内核制作
- 计算机网络面试题1
- 南阳OJ106-背包问题(贪心算法)
- plsql函数大全
- 常见HTTP错误代码大全