edu90 - D. Maximum Sum on Even Positions ( 策略 : 子数组的反转)

本文介绍了一道算法竞赛题目,目标是通过最多一次子数组翻转操作,最大化数组中偶数位置元素的总和。文章提供了完整的C++代码实现,采用动态规划思想,记录翻转前后的累积收益,最终输出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
在这里插入图片描述
大致题意:
给你一个大小为 n 的数组,最多执行操作一次:反转任意一个连续的子数组。
最后求 所有偶数位上的最大和。

解题报告:
在这里插入图片描述

#include<bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1e6+5;
const int inf=0x3f3f3f3f;
const LL MOD=1e9+7;

int p[maxn];
LL sum1[maxn],sum2[maxn];

int main() {
    int T, n;
    scanf("%d", &T);
    while (T--) {
        LL ans = 0;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &p[i]);
            if (i % 2 == 0) ans += p[i];
        }
        LL tmp1 = 0, ant = 0, tmp2 = 0;
        for (int i = 0; i < n; i += 2) {
            if (i + 1 < n) {
                tmp1 += p[i + 1] - p[i];
                if (tmp1 < 0) tmp1 = 0;
                ant = max(ant, tmp1);
            }
            if (i != 0) {
                tmp2 += p[i - 1] - p[i];
                if (tmp2 < 0) tmp2 = 0;
                ant = max(ant, tmp2);
            }
        }
        printf("%lld\n", ans + ant);
    }
    return 0;
}
`cesium` 的 `Primitive` 基础对象是用于绘制各种几何图形的对象,包括点、线、面等。其中,动态河流是一种特殊的线型图形,可以使用 `PolylineCollection` 和 `PolylineDashMaterialProperty` 来实现。 下面是一个使用 `PolylineCollection` 和 `PolylineDashMaterialProperty` 实现动态河流的示例代码: ```javascript const viewer = new Cesium.Viewer('cesiumContainer'); const positions = [ new Cesium.Cartesian3(x1, y1, z1), new Cesium.Cartesian3(x2, y2, z2), // ... ]; const style = { image: "./img/textures/poly-rivers.png", width: 280, height: 30, speed: 10 }; const polyCollection = new Cesium.PolylineCollection(); polyCollection.add({ positions, width: style.width, material: new Cesium.PolylineDashMaterialProperty({ image: style.image, color: Cesium.Color.WHITE, dashLength: style.width / 2, dashPattern: 255, repeat: new Cesium.Cartesian2(1, style.height / style.width), animationSpeed: style.speed }) }); viewer.scene.primitives.add(polyCollection); ``` 需要注意的是,上述代码中的 `x1, y1, z1, x2, y2, z2` 等变量需要根据实际情况进行替换。此外,还需要根据实际情况进行样式参数的设置,例如 `image` 图片路径、`width` 线宽、`height` 纹理高度、`speed` 动画速度等。 另外,如果需要动态更新动态河流的位置,可以通过修改 `polyCollection` 中的 `positions` 属性来实现,例如: ```javascript polyCollection.get(0).positions = [ new Cesium.Cartesian3(x1, y1, z1), new Cesium.Cartesian3(x2, y2, z2), // ... ]; ``` 其中,`get(0)` 表示获取 `polyCollection` 中的第一个动态河流对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值