Codeforces #253 (Div. 1)C. Artem and Array ( 贪心

C. Artem and Array

题目描述

Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn’t have an adjacent number to the left or right, Artem doesn’t get any points.

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

输入

The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

输出

In a single line print a single integer — the maximum number of points Artem can get.

样例

Examples
inputCopy
5
3 1 5 2 6
output
11
inputCopy
5
1 2 3 4 5
output
6
inputCopy
5
1 100 101 100 1
output
102

题意

给一组数据, 一个数如果两边比它大 那么就删掉这个数,并求出 sum+=min() s u m + = m i n ( 左 边 数 , 右 边 数 )
维护一个vector,将数字删成一个非递增或者非递减数列 ,得到的这个数列的最大次大值明显得不到
时间复杂度应该是 O(nlogn) O ( n l o g n )

AC代码

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector> 
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ls st<<1
#define rs st<<1|1
#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXN = 1e3+11;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
vector<int> v;
int main() {

    ios::sync_with_stdio(false);
    int n;
    LL sum = 0;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int x; 
        cin >> x;
        if(v.size() == 0) 
            v.push_back(x);
        else {
            int l = v.size();
            if(l>1 && x>=v[l-1] && v[l-2]>=v[l-1]) {
                while(x>=v[l-1] && v[l-2]>=v[l-1]) {
                    sum += min(v[l-2],x);
                    v.pop_back();
                    l--;
                    if(l == 1) 
                        break;
                }
                v.push_back(x);     
            }
            else
                v.push_back(x);
        }

    }
    int xx, yy;
    xx = yy = 0;
    for(int i = 0 ; i < v.size(); i++) {
        if(v[i] > xx) {
            yy = xx;
            xx = v[i];
        }
        else if(v[i] > yy) 
            yy = v[i];
        sum += v[i];
    }
    cout << sum-xx-yy << endl;
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值