蓝桥杯小白赛第六期 3.数学奇才

3.数学奇才【算法赛】 - 蓝桥云课 (lanqiao.cn)

这是我的代码,过了样例:

js 复制代码
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 1e9 + 5;
const LL T = -1e9 + 10;
LL sum = 0;
vector<LL> temp;
int cnt = 0;
int flag = 0;
int max_n = -1;
int a[100000005];

int main() {
    int n; cin >> n;

    for (int i = 0; i <n; i++)cin >> a[i];

    for (int i = 0; i < n; i++) {
        if (a[i] < 0) {
            cnt++;
        }
        else if (a[i] > 0) {
            temp.push_back(cnt);
            cnt = 0;
        }
    }

    if (temp.size() > 0) { // 确保 temp 不为空
        int index = 0;
        for (int i = 0; i < temp.size(); i++) {
            if (temp[i] > max_n) {
                max_n = temp[i];
            }
        }


        for (int i = 0; i < max_n; i++)
        {
            a[i] *= -1;
        }


        for (auto& it :a)sum += it;
    }

    cout << sum << endl;
    return 0;
}

这是人家的代码:

js 复制代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    vector<ll> a(n);
    ll sum=0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum+=abs(a[i]);
    }
    cout<<sum<<endl;
    return 0;
}
相关推荐
csdn_aspnet1 天前
C++ Lomuto分区算法(Lomuto Partition Algorithm)
开发语言·c++·算法
ZPC82101 天前
Open3D 与yolo-3d 那个更适合生成物体3d 包围盒
人工智能·算法·计算机视觉·机器人
行走的陀螺仪1 天前
JavaScript 算法详解:10大经典算法,通俗易懂,从入门到精通
开发语言·javascript·算法
1368木林森1 天前
RAG查询改写②【第十篇】:HYDE、StepBack、子问题拆分,高阶改写算法生产落地
人工智能·算法·rag
smj2302_796826521 天前
解决leetcode第3934题最短唯一子数组
数据结构·python·算法·leetcode
NashSKY1 天前
EPnP 算法详解
算法·矩阵分解·多视图几何·射影几何
小O的算法实验室1 天前
2026年SEVC,自适应模因算法+复杂约束条件下多无人机协同任务分配,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
iiiiyu1 天前
面向对象和集合编程题
java·开发语言·前端·数据结构·算法·编程语言
xiaoxiaoxiaolll1 天前
Light首次发表:动量空间穆勒矩阵偏振测量,破解纳米手性结构表征难题
人工智能·算法
变量未定义~1 天前
最长回文子串
数据结构·算法