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(左边数,右边数)sum+=min(左边数,右边数)
维护一个vector,将数字删成一个非递增或者非递减数列 ,得到的这个数列的最大次大值明显得不到
时间复杂度应该是O(nlogn)O(nlogn)
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;
}

本文介绍了一道算法题目,玩家需要从一个正整数数组中移除元素以获得最大分数,每次移除元素可以获得其相邻元素中的最小值。文章提供了问题描述、输入输出示例及AC代码实现。
618

被折叠的 条评论
为什么被折叠?



