[USACO13NOV] Pogo-Cow

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=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
    sort(e+1,e+n+1,cmp);
    for(int i=1;i<=n;i++)
     for(int j=0;j<i;j++)
     {
         for(int k=1;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][0]);
         dp1[i][j]+=e[i].v;
         dp2[i][j]=max(dp2[i][j],dp2[j][0]);
         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+1;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+1;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=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
    sort(e+1,e+n+1);
    for(int i=1;i<n;i++)
     for(int j=i+1;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+1;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+1;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=1;i<=n;i++) scanf("%d%d",&e[i].x,&e[i].v);
    sort(e+1,e+n+1);
    for(int i=1;i<n;i++)
     for(int j=i+1;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);
}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7380743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值