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. 【Ruby on Rails学习二】在线学习资料的整理

    由于工作任务重,时间紧,没有太多学习的时间,大致找了些在线学习资料,这里做个整理,希望对同样准备学习的朋友有帮助 在线文档类: Ruby on Rails 实战圣经  使用 Rails 4.2 及 R ...

  2. JS~模拟表单在新窗口打开,避免广告拦截

    说起广告拦截,这应该是浏览器的一个特性,它会将window.open产生的窗口默认为一个广告,将它进行拦截,但有时,这不是我们所希望的,有时,我们就是需要它在客户端的浏览器上弹出一个新窗口,以展示数据 ...

  3. Showing 2 changed files with 3 additions and 3 deletions.

    4  lib/matplotlib/__init__.py View   @@ -126,9 +126,9 @@ def compare_versions(a, b):     else: ...

  4. Redis起步

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介  Redis是一个key-value存储系统.和Memcached类似,但是解决 ...

  5. C#winform检测电脑安装的.netframework版本和是否安装了某软件

    代码如下: //C#获取已安装 .NET Framework 版本 private static string[] GetDotNetVersions() { DirectoryInfo[] dire ...

  6. C#委托的回调机制

    代码如下: public partial class FrmMain : Form { // 定义回调使用关键字 delegate(回调是委托的一种应用,其本质就是委托) private delega ...

  7. centos7编译安装MySQL5.7.9

    title: centos7编译安装MySQL5.7.9 date: 2016-05-12 16:20:17 tags: --- Centos7编译安装mysql5.7.9 mysql5.7有更好的性 ...

  8. canvas--画宇宙

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  9. 【回顾整理】HTML+CSS个的两个实战项目

    一:麦子商城首页制作 代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...

  10. Edwin windows下基本命令:

    Ctrl-Alt-z: 对区域内所有代码求值. Ctrl-x Ctrl-e: 对光标左边或上一个表达式求值. Ctrl-c Ctrl-x: 中断当前求值. Ctrl-a: 移动到行首. Ctrl-e: ...