列表

详情


NC24769. [USACO 2010 Ope G]Cow Hopscotch

描述

The cows have reverted to their childhood and are playing a game similar to human hopscotch. Their hopscotch game features a line of N (3 <= N <= 250,000) 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  V_i (-2,000,000,000 <= V_i <= 2,000,000,000). 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 (2 <= K <= N) 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: V_i

输出描述

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

示例1

输入:

6 3 
0 
1 
2 
-3 
4 
5 

输出:

12

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 76ms, 内存消耗: 9164K, 提交时间: 2020-06-15 22:09:16

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N=1e6+10;
LL f[N],a[N],s[N];
int q[N],hd,tl;
int n,k;
int main()
{
    int i;
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)
    scanf("%lld",a+i),s[i]=s[i-1]+max(a[i],0ll);
    LL ans=0;
    q[hd=tl=1]=-1;
    for(i=1;i<=n;i++)
    {
        while(hd<=tl&&i-q[hd]>k) ++hd;
        f[i]=f[max(0,q[hd])]+a[i]+s[i-1]-s[q[hd]+1],ans=max(ans,f[i]+max(a[i+1],0ll)),f[i]+=a[i+1];
        while(hd<=tl&&f[i-1]>f[max(0,q[tl])]+s[i]-s[q[tl]+1]) tl--;
        q[++tl]=i-1;
    }
    cout << ans << endl;
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 76ms, 内存消耗: 6740K, 提交时间: 2020-02-26 20:14:15

#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=1e6+10;
LL f[N],a[N],s[N];
int q[N],hd,tl;
int n,k;
int main()
{
	int i;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
	scanf("%lld",a+i),s[i]=s[i-1]+max(a[i],0ll);
	LL res=0;
	q[hd=tl=1]=-1;
	for(i=1;i<=n;i++)
	{
		while(hd<=tl&&i-q[hd]>k) ++hd;
		f[i]=f[max(0,q[hd])]+a[i]+s[i-1]-s[q[hd]+1],res=max(res,f[i]+max(a[i+1],0ll)),f[i]+=a[i+1];
		while(hd<=tl&&f[i-1]>f[max(0,q[tl])]+s[i]-s[q[tl]+1]) tl--;
		q[++tl]=i-1;
	}
	printf("%lld\n",res);
	return 0;
}

上一题