cf442C Artem and Array
来源:互联网 发布:seo基础视频教程 编辑:程序博客网 时间:2024/06/09 20:16
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.
53 1 5 2 6
11
51 2 3 4 5
6
51 100 101 100 1
102
贪心啊……
爆出内幕:昨天cf比赛的时候,zld大神认为此题贪心,结果没开long long第10个点wa。于是卓神信誓旦旦的说,这题贪心有反例。呵呵呵……
首先“填坑”:把两边都比中间大的数直接合掉,结果它就变成单调栈……
维护单调栈,最后sort一遍更新答案
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int n,top,zhan[500001];long long ans;int main(){scanf("%d",&n);for (int i=1;i<=n;i++){int x;scanf("%d",&x);while (top>1 && zhan[top-1]>=zhan[top]&& zhan[top]<=x) {ans+=min(zhan[top-1],x);top--;}zhan[++top]=x;}sort(zhan+1,zhan+top+1);for (int i=1;i<=top-2;i++){ans+=zhan[i];}printf("%lld",ans);}
- cf442C Artem and Array
- CodeForces 442C Artem and Array(贪心)
- Codeforces 442C Artem and Array(stack+贪心)
- Little Artem and Grasshopper
- Codeforces Round #253 (Div. 2) E. Artem and Array (思维)
- B. Little Artem and Grasshopper
- A. Little Artem and Presents
- CF Round #348 Div. 2 A. Little Artem and Present B. Little Artem and Grasshopper C. Little Artem and
- CodeForces 669A Little Artem and Presents
- CodeForces 669B Little Artem and Grasshopper
- CodeForces 669D Little Artem and Dance
- CodeForces 669D Little Artem and Dance
- CodeForces 668B Little Artem and Dance
- CodeForces 669ALittle Artem and Presents
- CodeForces 669A Little Artem and Presents
- 【CodeForces】669A - Little Artem and Presents
- 【CodeForces】[669A]Little Artem and Presents
- CodeForces 668A Little Artem and Matrix
- activity生命周期
- Java序列化与反序列化
- python 远程线程注入代码
- 习惯养成
- 创建cocos2d-x+lua项目
- cf442C Artem and Array
- mysql replace的使用
- 习题3-3 数数字 UVa1225
- 中英携手的全球意义 李克强展示中国形象
- R900086516 PGH5-2X/080RR11VU2
- sdfsdiogfdhgkjd
- .NET获取windows域帐号
- 程序员在大学里究竟应该学习什么?
- setsockopt()