UVALive 5790 Ball Stacking DP
DP的方向真的很重要,这题做的时候死活想不出来,看了题解以后恍然大悟原来这么简单。
题意:
有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取。求选的球最大的权值和。
题解:
把原来的金字塔变换一下形式,转换成直角三角形的样子。
假如原三角形是这样的:

然后可以转化成这个样子:

这样子的话,要选择一个球,就要选择他左上的那个矩形。
这样就容易转移了,用dp[i][j]表示变换后的图形中,第 i 行取的最下面一个球是j,很明显这样下一行就只能小于j个球了。
不过,这样每层之间的转移是O(n^2)的,也许会T,然后可以用单调性来优化一下。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = ; int a[MAXN][MAXN];
int sum[MAXN][MAXN];
int dp[MAXN][MAXN];
int n; int main() {
#ifdef Phantom01
freopen("LA5790.txt", "r", stdin);
#endif //Phantom01 while (scanf("%d", &n)!=EOF) {
if (n==) break;
memset(dp, , sizeof(dp));
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++)
for (int j = ; j <= i; j++)
scanf("%d", &a[i-j+][j]);
for (int i = ; i <= n; i++)
for (int j = ; j <= n-i+; j++)
sum[i][j] = a[i][j] + sum[i][j-] + sum[i-][j] - sum[i-][j-];
int ans = ;
for (int i = ; i <= n; i++) {
int best = n-i+;
for (int j = n-i+; j > ; j--) {
if (dp[i-][j]>dp[i-][best]) best = j;
dp[i][j] = dp[i-][best]+sum[i][j] - sum[i-][j];
ans = max(ans, dp[i][j]);
}
}
printf("%d\n", ans);
} return ;
}
UVALive 5790 Ball Stacking DP的更多相关文章
- UVALive 5790 Ball Stacking 解题报告
比赛总结 题目 题意: 有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取.求选的球最大的权值和. 题解: 将这堆球转成举行,第一行是(0,0),第二个是(1,0) ...
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- 【Gym 100015B】Ball Painting(DP染色)
题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...
- 【AtCoder】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- UVALive - 6952 Cent Savings dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...
- UVALive - 6529 找规律+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...
- UVaLive 6801 Sequence (计数DP)
题意:给定一个序列,有 n 个数,只有01,然后你进行k次操作,把所有的1变成0,求有多种方法. 析:DP是很明显的,dp[i][j] 表示进行第 i 次操作,剩下 j 个1,然后操作就两种,把1变成 ...
- UVaLive 6697 Homework Evaluation (DP)
题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第i个恰好匹配,则 +8,:如果不匹配,可以给长或短字符串添加-,先后匹配,这样-3, 连续的长字符串添加-,需要减去一个4:也可不给添加-,则 ...
- UVaLive 7374 Racing Gems (DP,LIS)
题意:以辆赛车可以从x轴上任意点出发,他的水平速度允许他向每向上移动v个单位,就能向左或向右移动v/r个单位(也就是它的辐射范围是个等腰三角形) 现在赛车从x轴出发,问它在到达终点前能吃到的最多钻石. ...
随机推荐
- Golang环境配置Centos
1.下载go程序包( go1.7rc1.linux-amd64.tar.gz)(http://www.golangtc.com/static/go/1.7rc1/go1.7rc1.linux-am ...
- 《鸟哥的Linux私房菜》笔记——01. 计算机概论
计算机的五大部分:输入单元.输出单元.CPU(控制单元(Control Unit).算数逻辑单元(ALU)).内存. CPU 的架构 精简指令集(Reduced Instruction Set Com ...
- AC自动机笔记
AC自动机 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...
- 51nod 1325 两棵树的问题(最大权闭合子图)
首先如果点权全都为正,就可以直接选所有的点. 活在梦里.. 考虑枚举一个点\(i\),作为我们选择的集合中的一个点. 然后我们把另一个点\(j\)选入集合的时候必须把两棵树中\(i\)和\(j\)路径 ...
- 3D ShapeNets: A Deep Representation for Volumetric Shapes 代码遇到的问题
遇到 Error using polygon2voxel_double Requested 515396075640x140711718551672x140719189273184 (17179869 ...
- 关闭linux终端进程
[root@linux-node1 ~]# w 22:16:45 up 24 days, 24 min, 2 users, load average: 0.28, 0.17, 0.15 USER TT ...
- CSS 子元素选择器
所有选择器参考手册:http://www.w3school.com.cn/cssref/css_selectors.asp 1)子元素选择器 参考链接:http://www.w3school.com. ...
- WPF打字机效果
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); TypewriteTex ...
- Apache activemq入门示例(maven项目)
http://outofmemory.cn/java/mq/apache-activemq-demo
- spark一些入门资料
spark一些入门资料 A Scala Tutorial for Java Programmers http://docs.scala-lang.org/tutorials/scala-for-jav ...