`
阿尔萨斯
  • 浏览: 4148024 次
社区版块
存档分类
最新评论

Codeforces 442C Artem and Array(stack+贪心)

 
阅读更多

题目连接:Codeforces 442C Artem and Array

题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分。问最大得分是多少。

解题思路:首先将连续的a,b,c,a>b&&c>b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分。

样例:4 10 2 2 8

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int N = 5 * 1e5 + 5;

int n, c = -1;
ll stack[N];

int main () {
    ll x, ans = 0;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%lld", &x);

        while (c > 0 && stack[c-1] >= stack[c] && stack[c] < x) {
            ans += min(stack[c-1], x);
            c--;
        }
        stack[++c] = x;
    }
    sort (stack, stack + c + 1);

    for (int i = 0; i <= c - 2; i++)
        ans += stack[i];
    printf("%lld\n", ans);
    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics