跳到正文

题解

题解:P12709 [KOI 2021 Round 1] 采蜜

wurang约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月6日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 2 分钟
题目
P12709

版本

  1. v1当前

分类讨论小水题。

本题分三种情况讨论。

  1. 蜂巢在最左,则必定有一只蜜蜂在最右,O(n)O(n) 枚举另一只蜜蜂,通过前缀和 O(1)O(1) 计算贡献。
  2. 蜂巢在最右则必定有一只蜜蜂在最左,O(n)O(n) 枚举另一只蜜蜂,通过后缀和 O(1)O(1) 计算贡献。
  3. 蜂巢在中间,则必定有一只蜜蜂在最右,有一只蜜蜂在最左,O(n)O(n) 枚举蜂巢,O(1)O(1) 计算贡献。

注意考虑两个蜜蜂不能在同一个位置

代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}while (ch >= '0' && ch <= '9'){x = x * 10 + ch - 48; ch = getchar();}return x * f;}

int n;
int a[100005];
int qz[100005];
int hz[100005];
int ans;

signed main()
{
	n = read();
	for(int i = 1; i <= n; i++)
		a[i] = read();
	for(int i = n; i >= 1; i--) // 后缀和计算
		hz[i] = hz[i+1] + a[i];
	for(int i = 1; i <= n; i++) // 前缀和计算
		qz[i] = qz[i-1] + a[i];
		
	for(int i = 2; i <= n; i++)
		ans = max(ans, hz[2] + hz[i + 1] - a[i]); // 情况1
	for(int i = n - 1; i >= 1; i--)
		ans = max(ans, qz[n - 1] + qz[i - 1] - a[i]); // 情况2
    for(int i = 2; i < n; i++)
    	ans = max(ans, qz[n] - a[1] - a[n] + a[i]); // 情况3
	cout << ans;
    return 0;
}
原文
存档评论1 条抓取于