Codeforces 442C
C. Artem and Arraytime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputIn a single line print a single integer — the maximum number of points Artem can get.
Sample test(s)Input5
3 1 5 2 6Output11Input5
1 2 3 4 5Output6Input5
1 100 101 100 1Output102
Accepted Code:
/*************************************************************************
> File Name: E.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年06月23日 星期一 22时48分29秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define min(x, y) ((x) < (y) ? (x) : (y))
typedef long long LL;
int n, a[];
int b[]; int
main(void) {
while (~scanf("%d", &n) && n) {
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
}
if (n <= ) {
puts(""); continue;
}
LL ans = ;
int cnt = ;
for (int i = ; i <= n; i++) {
b[cnt++] = a[i];
while (cnt > && b[cnt-] <= b[cnt-] && b[cnt-] <= b[cnt-]) {
ans += min(b[cnt-], b[cnt-]);
b[cnt-] = b[cnt-];
cnt--;
}
}
for (int i = ; i < cnt-; i++) {
ans += min(b[i-], b[i+]);
}
printf("%I64d\n", ans);
} return ;
}
Codeforces 442C的更多相关文章
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- codeforces 442C C. Artem and Array(贪心)
题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- Codeforces 442C Artem and Array (看题解)
Artem and Array 经过分析我们能发现, 如果对于一个a[ i ] <= a[ i + 1 ] && a[ i ] <= a[ i - 1 ]可以直接删掉. 最 ...
- Artem and Array CodeForces - 442C (贪心)
大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- Lombock原理
说道Lombok,可能会鲜为人知.但是在实际的开发中,它起到了很大的作用,话不多说,直入正题: 一.Lombok是什么 现在看一下Lombok官方对其进行的解释:Lombok官网:https://pr ...
- PageHelper原理
1.要是用pagehelper,首先maven项目,要引入 <dependency> <groupId>com.github.pagehelper</groupId> ...
- Apache SOLR and Carrot2集成
1.环境 下载软件 名称 地址 solr-integration-strategies-gh-pages.zip https://github.com/carrot2/solr-integration ...
- HZOI20190906模拟39 工业,卡常,玄学
题面:https://www.cnblogs.com/Juve/articles/11484209.html 工业: 推一个式子,AC 没有用组合数....推了2个多小时 我sbsbsbsbsbsbs ...
- 让pandoc输出pdf时支持中文
主机环境为:Ubuntu 12.04 LTS.对于RH系列,yum安装包的名称可能会有不同,不过yum联想能力比较强,应该不是问题. 安装pandoc,安装tex-live sudo apt-get ...
- [Day1] 初识Nginx
一. Nginx的诞生 Nginx是一个高效的web及反向代理服务器,它的第一版发布于2012年,晚于如今占据最大市场份额的Apache.那么Nginx的诞生肩负了哪些使命呢?或者说它于Apache的 ...
- Django之数据库连接与建模
Django数据库链接(这里以Mysql为例) 需要准备 Django1.10 pip install django==1.10 -i https://pypi.tuna.tsinghua.edu.c ...
- Redis 核心
一.Redis单机多实例原理 每个实例对应不同的配置文件,配置文件对应不同的端口.数据库文件位置.日志位置. 二.Redis单实例多数据库 每个Redis实例都有16个数据库,下标从0-15,当 se ...
- Odoo 在 Ubuntu 环境下性能调优
一.首先我们要分析影响odoo 服务器 性能的因素 CPU 目前大部分CPU在同一时间只能运行一个线程,超线程的处理器可以在同一时间处理多个线程,因此可以利用超线程特性提高系统性能. 在linux系统 ...
- 在rabbitmq操作页面上添加队列、交换器及绑定示图
1.添加队列 2.添加交换器 3.绑定