题解 | Cow Hopscotch-牛客假日团队赛1F题

登录—专业IT笔试面试备考平台_牛客网

https://ac.nowcoder.com/acm/contest/918/F

题目描述

The cows have reverted to their childhood and are playing a game similar to human hopscotch. Their hopscotch game features a line of N () squares conveniently labeled 1..N that are chalked onto the grass.

Like any good game, this version of hopscotch has prizes! Square i is labeled with some integer monetary value   (). The cows play the game to see who can earn the most money.

The rules are fairly simple:

* A cow starts at square '0' (located just before square 1; it has no monetary value).

* She then executes a potentially empty sequence of jumps toward square N. Each square she lands on can be a maximum of K () squares from its predecessor square (i.e., from square 1, she can jump outbound to squares 2 or 3 if K==2).

* Whenever she wishes, the cow turns around and jumps back towards square 0, stopping when she arrives there. In addition to the restrictions above (including the K limit), two additional restrictions apply:

* She is not allowed to land on any square she touched on her outbound trip (except square 0, of course).

* Except for square 0, the squares she lands on during the return trip must directly precede squares she landed on during the outbound trip (though she might make some larger leaps that skip potential return squares altogether).

She earns an amount of money equal to the sum of the monetary values of all the squares she jumped on. Find the largest amount of cash a cow can earn.

By way of example, consider this six-box cow-hopscotch course where K has the value 3:
Square Num:    0        1       2         3       4        5       6
                      +---+  +---+  +---+  +---+  +---+  +---+  +---+
                        |///|--|      |--|      |--|     |--|      |--|     |--|     |
                      +---+  +---+  +---+  +---+  +---+  +---+  +---+
     Value:          -        0        1        2       -3       4        5
One (optimal) sequence Bessie could jump (shown with respective bracketed monetary values) is: 1[0], 3[2], 6[5], 5[4], 2[1], 0[0] would yield a monetary total of 0+2+5+4+1+0=12.
If Bessie jumped a sequence beginning with 0, 1, 2, 3, 4, ... then she would be unable to return since she could not legally jump back to an untouched square.

输入描述:

* Line 1: Two space separated integers: N and K
* Lines 2..N+1: Line i+1 contains a single integer:

输出描述:

* Line 1: A single line with a single integer that is the maximum amount of money a cow can earn

示例1

输入
6 3 



-3 


输出
12

解答

dp维护从这个点开始往回跳的最大值,维护一个正数的前缀和,贪心的发现如果往回跳从跳到,其中的正数一定会被全部取掉。

写出一个的dp之后上单调队列降复杂度即可。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++) 
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f)) 
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x); 
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x); 
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long 
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();}
while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;}
const double eps = 1e-9;
const int maxn = 3e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int N,M,K;
LL a[maxn];
LL dp[maxn];
LL pre[maxn];
LL Queue[maxn];
int main(){
    Sca2(N,K);
    for(int i = 1; i <= N ; i ++){
         Scl(a[i]);
         if(a[i] <= 0) pre[i] = pre[i - 1];
         else pre[i] = pre[i - 1] + a[i];
    }
    int f = 1,t = 0;
    for(int j = 2; j <= N ; j ++){   //dp[i] + a[i + 1] - pre[i + 1]
        while(f <= t && Queue[f] < j - K) f++;
        if(j - 2 >= 0){
            while(f <= t && dp[Queue[t]] - pre[Queue[t]] <= dp[j - 2] - pre[j - 2]) t--;
            Queue[++t] = j - 2;
        }
        dp[j] = dp[Queue[f]] - pre[Queue[f]] + a[j] + a[j - 1] + pre[j - 2];
    }
    LL ans = dp[1] + pre[min(1 + K,N)];
    for(int i = 2; i <= N ; i ++){
        dp[i] += pre[min(i + K - 1,N)] - pre[i];
        ans = max(ans,dp[i]);
    //if(i < N) ans = max(ans,dp[i] + a[i + 1]);
    }
    Prl(ans);
    return 0;
}

来源:Hugh_Locke
全部评论

相关推荐

2025-12-08 07:42
门头沟学院 Java
27届末九,由于是女生,身边人几乎没有就业导向的,自学只能跟着网课,没人指导,很迷茫。下图是我目前的简历,不知道有需要修改的地方吗?求拷打。下面是目前的学习情况:目前算法过完了一遍力扣100和代码随想录,不过不是很熟,面经看了小林coding、JavaGuide,有一些没用过的技术看得不是很明白,掌握得不是很扎实。再加上常年跟黑马网课听思路,真正自己动手写代码的时间很少,这让我一直不敢投简历,总觉得内里空虚。项目没准备好面试相关的问题,简历上相应的考点不熟。如此种种。。。看到很多很多学长学姐大佬们的面经,愈发觉得面试可怕,自己没准备好,总担心自己是不是无望后端开发了。看到牛客很多同届以及更小一届的同学都找到实习了,很希望自己也能找到实习。而自己又好像摸不到后端学习的门路,只能不断赞叹黑马虎哥写的代码真优雅!微服务架构实在巧妙!消息队列、redis、sentinel、nacos、mybatisplus等等的引入都会让我赞叹这些工具的设计者的巧思,以及包括但不限于Java语言的优雅。然而只是停留在了解的程度,并不熟练。我是很希望能够继续深入探索这些知识的,只不过有一大部分时间都花在学校课程上了。我感觉我被困住了,我一方面必须保证我能够有个不错的学业分使我能有我几乎不想选择的读研退路(还有个原因是复习不全我会焦虑考试挂科,因此我会做好全面的准备,而这一步很费时间),一方面在B站学习各种网课,一方面得考虑提升自己并不扎实的算法基础,另一方面还得准备八股面经。这让我有点苦恼,我好像没那么多时间,因为绝大部分时间都花在了复习学校科目中了。我好像处处用时间,但收效甚微。想问问各位大佬是怎么平衡时间的呢?算法、项目和八股是怎么准备的呢?有什么高效的方法吗?谢谢您们花时间阅读我的稿件!
菜菜狗🐶:大胆投,我当时也是害怕面试,投多了发现根本约不到面🤡
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务