「CF442C」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 getsmin(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.
1 2 |
5 3 1 5 2 6 |
1 |
11 |
1 2 |
5 1 2 3 4 5 |
1 |
6 |
1 2 |
5 1 100 101 100 1 |
1 |
102 |
题解
首先填坑。。。也就是如果一个点俩边比它高把它删了。。。
然后序列整个序列递增或者递减。。。贪心即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define inf 0x7fffffff #define ll long long using namespace std; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int s[500005]; int n,top; ll ans; int main() { n=read(); for(int i=1;i<=n;i++) { int x=read(); while(top>1&&s[top-1]>=s[top]&&x>=s[top]) {ans+=min(s[top-1],x);top--;} s[++top]=x; } sort(s+1,s+top+1); for(int i=1;i<=top-2;i++)ans+=s[i]; printf("%I64d",ans); return 0; } |
分类目录
热门文章
- 12206NOIP1999拦截导弹
- 11713「BZOJ3261」最大异或和
- 11447NOIP2010导弹拦截
- 100872017 训练赛 1 by hzwer
- 9690「百度之星2017」程序设计大赛 初赛(B)
- 8585「JoyOI1048」田忌赛马
- 8528NOIP2010引水入城
- 84322017ACM萧山训练第3场(World Final 2013)
- 84242017ACM萧山训练第4场(CTUO 2015)
- 8251「CF718X」Codeforces Round #373 (Div. 1)
- 7887「BZOJ3709」[PA2014] Bohater
- 7865NOI2010 超级钢琴
- 7819「BZOJ1150」[CTSC2007] 数据备份Backup
- 7803「BZOJ1046」[HAOI2007] 上升序列
- 7741「BZOJ2811」[Apio2012] Guard
- 7675「BZOJ2288」「POJ Challenge」生日礼物
- 7488「BZOJ3166」[HEOI2013] Alo
- 7400「BZOJ2151」种树
- 7284「BZOJ3105」[CQOI2013] 新Nim游戏
- 7175「BZOJ4027」[HEOI2015] 兔子与樱花
- 7038「CF1280X」Codeforces Round #607 (Div. 1)
- 6995NOIP2012国王游戏
- 6923「BZOJ1086」[SCOI2005] 王室联邦
- 6916NOI2014随机数生成器
- 6865NOIP2007纪念品分组
- 6788「CF538X」Codeforces Round #300
- 6719「BZOJ1043」[HAOI2008] 下落的圆盘
- 6713POJ训练记录2
- 66762017ACM萧山训练第5场(2016 Pacific Northwest - Division 1)
- 63942015 ACM / ICPC EC - Final
- 6373「codechef」January Challenge 2015
- 6204NOIP2004合并果子
- 6187「BZOJ1229」[USACO2008 Nov] toy 玩具
- 6175NOI2009诗人小G
近期评论
- hzwer发表在《hzwer.com 博客导航》
- hzwer发表在《留言板》
- hzwer发表在《留言板》
- hzwer发表在《hzwer.com 博客导航》
- hzwer发表在《hzwer.com 博客导航》
整个序列单调应该是不对的吧……应该是单峰,但是单峰这样做是没问题的
嗯应该是单峰。。