三 . 弹簧高跷

时间限制: 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;
}

最新文章

  1. The current identity (NT AUTHORITY/NETWORK SERVICE)
  2. VS中使用svn注意事项
  3. 第五节:表单标签的用法——value绑定和修饰符
  4. Xcode如何找到默认的生成路径?
  5. java中的hashcode
  6. mysql 汉字乱码
  7. 解决JVM最大内存设置问题
  8. mouseover,mouseout,mouseenter,mouseleave的区别
  9. Hadoop概念学习系列之如何去找到历史版本的Hadoop发行包(三十四)
  10. js json和对象互相转换
  11. BZOJ 3994 约数个数和
  12. 【Linux】 任务调度/计划 cron
  13. 必须声明标量变量 &quot;@cid&quot;。
  14. 461. Hamming Distance(leetcode)
  15. JavaWeb面试(七)
  16. Terminating app due to uncaught exception &#39;CALayerInvalid&#39;, reason: &#39;layer &lt;CALayer: 0x7fda42c66e30&gt; is a part of cycle in its layer tree&#39;
  17. PHP的Memcached简单实现
  18. Selenium 3----获取断言信息
  19. sql server 学习笔记 ( row_number, rank, dense_rank over partition by order by )
  20. 基于TensorFlow的深度学习系列教程 2——常量Constant

热门文章

  1. Tmux常用命令总结
  2. Linux Cgroup v1(中文翻译)(3):CPU Accounting Controller
  3. Halodoc使用 Apache Hudi 构建 Lakehouse的关键经验
  4. @vue/cli3+配置build命令构建测试包&amp;正式包
  5. 几种常见的DoS攻击
  6. SAP OOALV- 合计
  7. vi与vim使用
  8. docker安装报错failure: repodata/repomd.xml from mirrors.aliyun.com_docker-ce_linux_centos_docker-ce.pro
  9. NC53681 「土」巨石滚滚
  10. 隐私计算FATE-多分类神经网络算法测试