做题记录 - CF1175D Array Splitting
题面
将
我们规定
心路历程
Round 1
由于相同的段中元素的
拆括号,得:
思路中断。
Round 2
考虑分段求和,
把
继续化简,得:
时间复杂度
空间复杂度
跑样例后发现WA了
核心代码:
read(n, k);
for(int i = 1; i <= n; ++i) {
read(a[i]);
s[i] = s[i - 1] + a[i];
}
for(int i = 1; i < k; ++i) tot += s[i];
cout << k * s[k] - tot;
Round DEBUG
检查推导,发现推导的第二步不合理。
Round 3
再推导一遍。
想要让这个式子最大,只需要让
不能动原始序列的顺序
核心代码:
read(n, k);
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i];
sort(s + 1, s + n);
for(int i = 1; i < k; ++i) tot += s[i];
write(k * s[n] - tot);
WA
Round DEBUG
算了一下数据范围,会爆int
十年OI一场空,不开long long见祖宗
AC Code
#pragma region
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <type_traits>
using namespace std;
using ll = long long;
using LL = ll;
const int INF = 0x7fffffff;
inline void IOS() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
}
template <typename T>
void read(T &x) {
x = 0; char c = getchar(); int f = 0;
for (; !isdigit(c); c = getchar())
f |= c == '-';
for (; isdigit(c); c = getchar())
x = x * 10 + (c ^ '0');
if (f) x = -x;
}
template <typename T, typename ... Args>
void read(T& a, Args&... args) {
read(a), read(args...);
}
template <typename T>
void write(T x, char ed = '\n') {
if(std::is_same<typename std::decay<T>, char>::value) {
putchar(x); return ;
}
if (x < 0) putchar('-'), x = -x;
static short st[30], tp;
do st[++tp] = x % 10, x /= 10; while (x);
while (tp) putchar(st[tp--] | '0');
putchar(ed);
}
template <typename T, typename ... Args>
void write(T& a, Args&... args) {
write(a), write(args...);
}
#pragma endregion
const int N = 5e5 + 10;
void Solve();
ll n, k;
vector<ll> a(N);
ll s[N];
ll tot = 0;
signed main() {
IOS();
Solve();
return 0;
}
void Solve_throw() {
read(n, k);
for(ll i = 1; i <= n; ++i) {
read(a[i]);
s[i] = s[i - 1] + a[i];
}
sort(s + 1, s + n);
for(ll i = 1; i < k; ++i) tot += s[i];
cout << k * s[n] - tot;
}
void Solve() {
read(n, k);
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i];
sort(s + 1, s + n);
for(int i = 1; i < k; ++i) tot += s[i];
write(k * s[n] - tot);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端