1313C1


题目分析

题意

给你一个数列,你可以减少某个元素的值,在不出现某项的值小于他周围两项的值的情况下,问你该数列的和最大的情况是什么样子的

通过题意可知,合法的数列的情况是整个数列的极大值只有一种,我们可以枚举数列的每个元素为极大值的情况,最后构造出结果

AC代码

#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long LL;
constexpr int N = 1e5 + 100;

int n;
LL a[N], b[N];
LL check(int x)
{
    LL sum = a[x];
    for (int i = x - 1; i; i--) 
        a[i] = min(a[i], a[i + 1]), sum += a[i];
    for (int i = x + 1; i <= n; i++)
        a[i] = min(a[i], a[i - 1]), sum += a[i];

    return sum;
}

int main()
{
    io;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];

    LL maxv = 0;
    int x = 0;
    for (int i = 1; i <= n; i++)
    {
        LL t = check(i);
        if (t > maxv) 
        {
            maxv = t;
            x = i;
        } 
        for (int i = 1; i <= n; i++) a[i] = b[i];
    }
    for (int i = x - 1; i; i--) a[i] = min(a[i], a[i + 1]);
    for (int i = x + 1; i <= n; i++) a[i] = min(a[i], a[i - 1]);
    for (int i = 1; i <= n; i++) cout << a[i] << ' ';

    return 0;
}
原文地址:https://www.cnblogs.com/FrankOu/p/15409459.html