弹簧高跷题解---双向DP---DD(XYX)的博客
2024-10-20 00:49:00
三 . 弹簧高跷
时间限制: 1 Sec 内存限制: 128 MB
题目描述、输入、输出
-----------
方法
这道题用DP是可以解决的。因为每一次跳跃都与前一次跳跃有关,也就是说,每次要枚举前两次的跳跃落点,然后才能计算出某点到一个落点的最优解。这样一来,就可以定义一个 f [ ] [ ] ,f[ i ][ j ] 表示 以 “ 从 i 点跳到 j 点 ” 结束 的 一整次跳跃过程 中,得到的最高分 (注意: 可能是 i < j,也可能是 i > j,当然也有 i = j 的情况)。状态转移方程:
(分数)
先处理 f[ i ][ i ] 的情况,然后正逆序都进行一次三重循环。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
void read(int &x) {
int f = 1;x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0';s = getchar();}
x *= f;
}
struct no{
int x,h;
}a[1005];
bool operator < (no a,no b) {
return a.x < b.x;
}
bool operator > (no a,no b) {
return b < a;
}
int n,m,s,o,i,j,k,ans = 0;
int dp[1005][1005];
int main() {
read(n);
for(i = 1;i <= n;i ++) {
read(a[i].x);read(a[i].h);
ans = max(ans,a[i].h);
}
sort(a + 1,a + 1 + n);
for(i = 1;i <= n;i ++) {
dp[i][i] = a[i].h;
for(j = 1;j < i;j ++) {
dp[j][i] = dp[j][j];
for(k = j - 1;k > 0 && a[j].x - a[k].x <= a[i].x - a[j].x;k --) {
dp[j][i] = max(dp[j][i],dp[k][j]);
}
dp[j][i] += a[i].h;
ans = max(ans,dp[j][i]);
}
}
for(i = n;i > 0;i --) {
for(j = n;j > i;j --) {
dp[j][i] = dp[j][j];
for(k = j + 1;k <= n && a[k].x - a[j].x <= a[j].x - a[i].x;k ++) {
dp[j][i] = max(dp[j][i],dp[k][j]);
}
dp[j][i] += a[i].h;
ans = max(ans,dp[j][i]);
}
}
printf("%d",ans);
return 0;
}
最新文章
- The current identity (NT AUTHORITY/NETWORK SERVICE)
- VS中使用svn注意事项
- 第五节:表单标签的用法——value绑定和修饰符
- Xcode如何找到默认的生成路径?
- java中的hashcode
- mysql 汉字乱码
- 解决JVM最大内存设置问题
- mouseover,mouseout,mouseenter,mouseleave的区别
- Hadoop概念学习系列之如何去找到历史版本的Hadoop发行包(三十四)
- js json和对象互相转换
- BZOJ 3994 约数个数和
- 【Linux】 任务调度/计划 cron
- 必须声明标量变量 ";@cid";。
- 461. Hamming Distance(leetcode)
- JavaWeb面试(七)
- Terminating app due to uncaught exception &#39;CALayerInvalid&#39;, reason: &#39;layer <;CALayer: 0x7fda42c66e30>; is a part of cycle in its layer tree&#39;
- PHP的Memcached简单实现
- Selenium 3----获取断言信息
- sql server 学习笔记 ( row_number, rank, dense_rank over partition by order by )
- 基于TensorFlow的深度学习系列教程 2——常量Constant
热门文章
- Tmux常用命令总结
- Linux Cgroup v1(中文翻译)(3):CPU Accounting Controller
- Halodoc使用 Apache Hudi 构建 Lakehouse的关键经验
- @vue/cli3+配置build命令构建测试包&;正式包
- 几种常见的DoS攻击
- SAP OOALV- 合计
- vi与vim使用
- docker安装报错failure: repodata/repomd.xml from mirrors.aliyun.com_docker-ce_linux_centos_docker-ce.pro
- NC53681 「土」巨石滚滚
- 隐私计算FATE-多分类神经网络算法测试