zoj2059(经典dp)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1059
分析:dp[i][j]表示前i个石头组成两座塔高度差为j的较低塔最大高度
状态转移:
每次石头都有三种方法:
1.放在高塔上:dp[i][j]=max(dp[i][j+t],dp[i][j]);低塔不变
2.放在低塔上:1)dp[i][j]=max(dp[i][j-t],dp[i][j]);低塔仍是较低塔
2)dp[i][j]=max(dp[i][t-j],dp[i][j]);低塔超过高塔
3.低塔高塔都不放
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
using namespace std;
int dp[][];
int main()
{
int n,t;
while(scanf("%d",&n)>)
{
if(n<)break;
memset(dp,-,sizeof(dp));
dp[][]=;
for(int i=;i<=n;i++)
{
scanf("%d",&t);
memcpy(dp[i],dp[i-],sizeof(dp[i-]));
for(int j=;j<=;j++)
{ if(dp[i-][j]!=-)
{
int x=dp[i-][j];
int y=dp[i-][j]+j;
if(x+t<=y)
{
dp[i][j-t]=max(dp[i][j-t],x+t);
}
else
{
dp[i][t-j]=max(dp[i][t-j],y);
}
dp[i][j+t]=max(dp[i][j+t],x);
}
}
}
if(dp[n][]<=)puts("Sorry");
else printf("%d\n",dp[n][]);
}
}
为了节省空间,还可以用滚动数组来实现dp
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define inf 1<<30
#define mod 1000000007
using namespace std;
int dp[][];
int main()
{
int n,t;
while(scanf("%d",&n)>)
{
if(n<)break;
memset(dp,-,sizeof(dp));
dp[][]=;
int pre=,now=;
for(int i=;i<=n;i++)
{
scanf("%d",&t);
memcpy(dp[now],dp[pre],sizeof(dp[now]));
for(int j=;j>=;j--)
{
if(dp[pre][j]!=-)
{
int x=dp[pre][j];
int y=dp[pre][j]+j;
if(x+t<=y)
{
dp[now][j-t]=max(dp[now][j-t],x+t);
}
else
{
dp[now][t-j]=max(dp[now][t-j],y);
}
dp[now][j+t]=max(dp[now][j+t],x);
}
}
swap(pre,now);
}
if(dp[pre][]<= )puts("Sorry");
else printf("%d\n",dp[pre][]);
}
}
zoj2059(经典dp)的更多相关文章
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...
- NYOJ - 矩形嵌套(经典dp)
矩形嵌套时间限制:3000 ms | 内存限制:65535 KB 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b< ...
- 51nod 1412 AVL树的种类(经典dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k] ...
- NYOJ 16 矩形嵌套(经典DP)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- CS Academy Distinct Neighbours(经典dp)
CS Academy Distinct Neighbours(经典dp) 题意: 求相邻无相同数字的合法的排列数 题解: 题解 先将相同的数字分为一类,假设共有n组 定义\(dp[i][j]\)表示前 ...
- 【经典dp 技巧】8.13序列
经典的拆绝对值 题目大意 给定$n$个具有顺序的序列,允许对每个序列循环移动.记第$i$个序列尾元素为$x$,$i+1$个序列首元素为$y$,定义其连接收益为$|x-y|*i$,求$n$个序列连接最大 ...
- POJ 1160:Post Office 邮局经典DP
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17168 Accepted: 9270 Desc ...
随机推荐
- cocos2d-x游戏开发系列教程-坦克大战游戏启动界面的编写
用前面介绍的方法,创建一个cocos2d-x项目,可以看到新项目内容如下图:
- phpcms 列表页中,如何调用其下的所有子栏目(或特定的子栏目)?
{pc:get sql="select * from phpcms_category where catid in(你的子栏目ID)" return="data" ...
- Eclipse设置Android Logcat输出字体大小
Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...
- [课堂实践与项目]IOS优先级的计算器
这个计算器主要是使用数组进行实现的.虽然没有使用前缀后缀表达式,但是是一种方法o. .h文件 // // LCViewController.h // 具有优先级的calculator // // Cr ...
- UVA 311 Packets 贪心+模拟
题意:有6种箱子,1x1 2x2 3x3 4x4 5x5 6x6,已知每种箱子的数量,要用6x6的箱子把全部箱子都装进去,问需要几个. 一开始以为能箱子套箱子,原来不是... 装箱规则:可以把箱子都看 ...
- 使用malloc分别分配2KB,6KB的内存空间,打印指针地址
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...
- 让工程师爱上CMMI,实现管理于无形 --- 中标软件CMMI L5之路 (1/2)
操作系统市场被微软等国外的IT厂商垄断的大环境下,中标软件作为市场夹缝中发展起来的民族企业,致力于成为中国操作系统旗舰企业.系列核心产品已经在政府.金融.教育.财税.公安.审计.交通.医疗.制造等行业 ...
- Python IDLE 快捷键
Python IDLE 快捷键 编辑状态时: Ctrl + [ .Ctrl + ] 缩进代码 Alt+3 Alt+4 注释.取消注释代码行 Alt+5 Alt+6 切换缩进方式 空格<=> ...
- ZeroMQ:云计算时代最好的通讯库
还在学socket编程吗?还在研究为什么epoll比select更好吗? 噢,不必了! 在复杂的云计算环境中,我们面临的难题远比这个复杂得多. 庞大的服务器集群作为计算云,对来来看或许只是一个简单的搜 ...
- mysql5.6 主从配置
参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...