https://www.luogu.org/problem/show?pid=3089

题目描述

In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John has attached a pogo stick to each of Bessie's legs. Bessie can now hop around quickly throughout the farm, but she has not yet learned how to slow down.

To help train Bessie to hop with greater control, Farmer John sets up a practice course for her along a straight one-dimensional path across his farm. At various distinct positions on the path, he places N targets on which Bessie should try to land (1 <= N <= 1000). Target i is located at position x(i), and is worth p(i) points if Bessie lands on it. Bessie starts at the location of any target of her choosing and is allowed to move in only one direction, hopping from target to target. Each hop must cover at least as much distance as the previous hop, and must land on a target.

Bessie receives credit for every target she touches (including the initial target on which she starts). Please compute the maximum number of points she can obtain.

FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度。

FJ觉得让贝西在一条直线的一维线路上进行练习,他在不同的目标点放置了N (1 <= N <= 1000)个目标点,目标点i在目标点x(i),该点得分为p(i)。贝西开始时可以选择站在一个目标点上,只允许朝一个方向跳跃,从一目标点跳到另外一个目标点,每次跳跃的距离至少和上一次跳跃的距离相等,并且必须跳到一个目标点。

每跳到一个目标点,贝西可以拿到该点的得分,请计算他的最大可能得分。

输入输出格式

输入格式:

  • Line 1: The integer N.

  • Lines 2..1+N: Line i+1 contains x(i) and p(i), each an integer in the range 0..1,000,000.

输出格式:

  • Line 1: The maximum number of points Bessie can receive.

输入输出样例

输入样例#1:

6
5 6
1 1
10 5
7 6
4 8
8 10
输出样例#1:

25

说明

There are 6 targets. The first is at position x=5 and is worth 6 points, and so on.

Bessie hops from position x=4 (8 points) to position x=5 (6 points) to position x=7 (6 points) to position x=10 (5 points).

82分做法:

dp[i][j] 表示 i是由j转移过来的最大得分

枚举k转移

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1001
using namespace std;
int n,ans,dp1[N][N],dp2[N][N];
struct node
{
int x,v;
}e[N];
bool cmp(node p,node q)
{
return p.x<q.x;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
sort(e+,e+n+,cmp);
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
{
for(int k=;k<j;k++)
{
if(e[i].x-e[j].x>=e[j].x-e[k].x) dp1[i][j]=max(dp1[i][j],dp1[j][k]);
if(e[i].x-e[j].x<=e[j].x-e[k].x) dp2[i][j]=max(dp2[i][j],dp2[j][k]);
}
dp1[i][j]=max(dp1[i][j],dp1[j][]);
dp1[i][j]+=e[i].v;
dp2[i][j]=max(dp2[i][j],dp2[j][]);
dp2[i][j]+=e[i].v;
ans=max(ans,max(dp1[i][j],dp2[i][j]));
}
printf("%d",ans);
}

55分做法:

记忆化搜索

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1001
using namespace std;
int n,ans,dp1[N][N],dp2[N][N];
struct node
{
int x,v;
bool operator < (node p)const
{
return x<p.x;
}
}e[N];
int dfs1(int s,int t,int dis)
{
if(dp1[s][t]) return dp1[s][t];
for(int i=t+;i<=n;i++)
if(e[i].x-e[t].x>=dis) dp1[s][t]=max(dp1[s][t],dfs1(t,i,e[i].x-e[t].x)+e[i].v);
return dp1[s][t];
}
int dfs2(int s,int t,int dis)
{
if(dp2[s][t]) return dp2[s][t];
for(int i=t+;i<=n;i++)
if(e[i].x-e[t].x<=dis) dp2[s][t]=max(dp2[s][t],dfs2(t,i,e[i].x-e[t].x)+e[i].v);
return dp2[s][t];
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
sort(e+,e+n+);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{ dp1[i][j]=dfs1(i,j,e[j].x-e[i].x)+e[i].v+e[j].v;
dp2[i][j]=dfs2(i,j,e[j].x-e[i].x)+e[i].v+e[j].v;
ans=max(ans,max(dp1[i][j],dp2[i][j]));
}
printf("%d\n",ans);
}

36分做法:

普通搜索

#include<cstdio>
#include<algorithm>
#define N 1001
using namespace std;
int n,ans;
struct node
{
int x,v;
bool operator < (node p)const
{
return x<p.x;
}
}e[N];
int dfs1(int s,int t,int dis,int sum)
{
ans=max(ans,sum);
for(int i=t+;i<=n;i++)
if(e[i].x-e[t].x>=dis) dfs1(t,i,e[i].x-e[t].x,sum+e[i].v);
}
int dfs2(int s,int t,int dis,int sum)
{
ans=max(ans,sum);
for(int i=t+;i<=n;i++)
if(e[i].x-e[t].x<=dis) dfs2(t,i,e[i].x-e[t].x,sum+e[i].v);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
sort(e+,e+n+);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
dfs1(i,j,e[j].x-e[i].x,e[i].v+e[j].v);
dfs2(i,j,e[j].x-e[i].x,e[i].v+e[j].v);
}
printf("%d\n",ans);
}

最新文章

  1. 远程桌面不能连接,提示awgina.dll取代错误的解决办法
  2. Unity自学路线整理(参看微信公众号Unity墙外的世界的文章 )
  3. Linux 守护进程二(激活守护进程)
  4. Android之Selector、Shape介绍
  5. day0
  6. gulp之css,js压缩合并加密替换
  7. HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结
  8. junit参数化测试的使用方法
  9. Android 安全加密
  10. android的Findbugs问题整理
  11. npm打包前端项目太慢问题分析以及暂时解决方案
  12. 服务器数据恢复_服务器xfs数据丢失数据恢复
  13. HO6 Condo Insurance Policy
  14. spring datasource 使用 proxool
  15. Destructuring Assignment in JS(解构assignment in js)
  16. TZOJ 1210 The area(微积分)
  17. WPF设置控件获取键盘焦点时的样式FocusVisualStyle
  18. HDU2859(KB12-Q DP)
  19. android listview 优化
  20. Ubuntu Linux下的Wireshark使用drcom_2011.lua分析drcom协议

热门文章

  1. ServiceStack.Ormlit 事务
  2. 接口_requests_基于python
  3. css3美化radio样式
  4. 软工实践Alpha冲刺(1/10)
  5. PHP给图片添加图片水印
  6. Java取两个变量不为空的变量的简便方法!
  7. WPF值转换实例
  8. 操作 使用XML的方法
  9. C++基础知识(二)
  10. Vika and Segments - CF610D