Acwing6118 蛋糕游戏

这是yxc大佬的题解,这道题的推导很有意思,但是结论很简单,就是用前缀和找中间一段累加最小的连续数组。唉是我一辈子做不出的题。

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;
const int N = 500010;

int n;
LL s[N];

int main()
{
    int T;
    scanf("%d", &T);

    while (T -- )
    {
        scanf("%d", &n);

        LL a = 1e15;
        int l = n / 2 + 1;
        for (int i = 1; i <= n; i ++ )
        {
            int x;
            scanf("%d", &x);
            s[i] = s[i - 1] + x;
            if (i >= l) a = min(a, s[i] - s[i - l]);
        }

        printf("%lld %lld\n", a, s[n] - a);
    }

    return 0;
}
相关推荐
电科_银尘5 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
我不会插花弄玉6 小时前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll
郝学胜-神的一滴8 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David8 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
西西弗Sisyphus9 小时前
C++ explicit 关键字工程实践
开发语言·c++
Pniubi11 小时前
力扣21合并两个有序链表
算法·leetcode·职场和发展
AIgorithmGEEK11 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
上海潭龙东海科技有限公司13 小时前
智慧堆场动态模型三维实时孪生重构智能管控
大数据·网络·数据库·算法·重构
一拳一个呆瓜13 小时前
【STL】头文件:<cmath>
c++·stl
草莓熊Lotso14 小时前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构