题解 P11233【[CSP-S 2024] 染色】
题目描述
给定一个长度为
你需要将
设
- 如果
左侧没有与其同色的数,则令 。 - 否则,记其左侧与其最靠近的同色数为
,若 ,则令 ,否则令 。
你的最终得分为
对于所有测试数据,保证:
solution
两种颜色是没有区别的。
将 push_back
,不会影响其它已计算的值),需要支持查询全局最大值和
code
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define endl "\n"
#endif
using LL = long long;
template <class T> T& cmax(T& x, const T& y) { return x = max(x, y); }
template <class T> T& cmin(T& x, const T& y) { return x = min(x, y); }
int n, a[200010];
LL buc[1000010];
int mian() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
memset(buc, ~0x3f, sizeof buc);
LL tag = 0, mx = 0;
buc[0] = 0;
for (int i = 2; i <= n; i++) {
LL val = max(mx, buc[a[i]] + tag + a[i]);
if (a[i] == a[i - 1]) tag += a[i], mx += a[i];
cmax(mx, val), cmax(buc[a[i - 1]], val - tag);
}
cout << mx << endl;
return 0;
}
int main() {
#ifndef LOCAL
#ifdef NF
freopen("color.in", "r", stdin);
freopen("color.out", "w", stdout);
#endif
cin.tie(nullptr)->sync_with_stdio(false);
#endif
int t;
cin >> t;
while (t--) mian();
return 0;
}
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/18526012/solution-P11233
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战