跳到正文

题解

题解:P11452 [USACO24DEC] Cake Game S

GUO120822约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1
题目
P11452

版本

  1. v1当前

第一次 AK 银组,发题解庆祝下。

发现两人取的个数固定。

维护前缀和,后缀和,取前 xx 个,后 n/2−1−xn/2-1-x 个即可。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
int T,n,i,k;
ll a[N],b[N];
ll sum,ans,x[N],y[N];
void work()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%lld",&a[i]);
	sum=ans=0;
	int i;
	for (i=1;i<=n;i++) sum+=a[i];
	x[0]=y[n+1]=0;
	for (i=1;i<=n;i++) x[i]=x[i-1]+a[i];
	for (i=n;i>=1;i--) y[i]=y[i+1]+a[i];
	for (i=0;i<=n/2-1;i++) ans=max(ans,x[i]+y[n-(n/2-1-i)+1]);
	printf("%lld %lld\n",sum-ans,ans);
}
int main()
{
	scanf("%d",&T);
	while (T--) work();
	return 0;
}
原文