C. Artem and Array
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

Output

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

Sample test(s)
input
5
3 1 5 2 6
output
11
input
5
1 2 3 4 5
output
6
input
5
1 100 101 100 1
output
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的更多相关文章

  1. [CF442C] Artem and Array (贪心+单调栈优化)

    题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...

  2. codeforces 442C C. Artem and Array(贪心)

    题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  4. Codeforces 442C Artem and Array (看题解)

    Artem and Array 经过分析我们能发现, 如果对于一个a[ i ] <= a[ i + 1 ] && a[ i ] <= a[ i - 1 ]可以直接删掉. 最 ...

  5. 「CF442C」 Artem and Array

    题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...

  6. codeforces 442C C. Artem and Array(有深度的模拟)

    题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...

  7. Artem and Array

    Codeforces Round #253 (Div. 1) C:http://codeforces.com/problemset/problem/442/C 题意:给你一个序列,然后你每次可以删除一 ...

  8. Artem and Array CodeForces - 442C (贪心)

    大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...

  9. Codeforces Round 253 (Div. 2)

    layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...

随机推荐

  1. eclipse svn插件安装

    1,在线直接安装 help --> Eclipse Marketplace 2,下载subclipse.zip 把features.plugins拷贝到eclipse安装目录 3,直接把subc ...

  2. js基础例子动态创建table实例

    <style> table{ width:500px; font-weight: bold; border: 1px solid #000; border-collapse:collaps ...

  3. Java使用poi包读取Excel文档

    项目需要解析Excel文档获取数据,就在网上找了一些资料,结合自己这次使用,写下心得: 1.maven项目需加入如下依赖: <dependency> <groupId>org. ...

  4. mysql中pager命令妙用

    pager命令的妙用在mysql,可以大大提高工作效率. 一 当处理大量数据时,不想显示查询的结果,而只需知道查询话费的时间. mysql> select *   from t3; +----- ...

  5. Java正則表達式语法

    Java正則表達式语法 字符 说明 \ 将下一字符标记为特殊字符.文本.反向引用或八进制转义符.比如,"n"匹配字符"n"."\n"匹配换行 ...

  6. 解密UML九中关系

    将UML中经常使用的九种关系分为了四组进行解释. 一.组合和聚合解说: 同样:均是指有部分组成总体. 不同:聚合是指能够独立存在的个体组成总体.(弱的拥有关系) 组合存在时间上的关系.总体和部分具有同 ...

  7. 用python演示一个简单的AST(抽象语法树)

    如果对'a + 3 * b'进行解释,当中a=2,b=5 代码非常easy,就不再进行具体的解释了. Num = lambda env, n: n Var = lambda env, x: env[x ...

  8. codeforce 437B The Child and Set

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. Linux下查看显卡型号

    查看显卡使用 lspci |grep VGAemos@emos-desktop:~$ lspci -vnn | grep -i vga00:02.0 VGA compatible controller ...

  10. NPM与调试工具的使用

    1)NPM 2)nodemon 自动监视文件的变化并重启应用 3)pm2 启动node,资源共享 4)node-inspector node调试工具 5)Chrome Developer Tools