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 ...
随机推荐
- org.apache.ibatis.executor.ExecutorException: No constructor found in xxxClass matching [java.lang.String, java.lang.Long, java.lang.String, java.lang.String, java.sql.Timestamp]
解决方案 为bean加上默认构造方法 public class User { private String username; private long id; private String sex; ...
- JS对象和数组深浅拷贝总结②
在实际开发中遇到过太多次深拷贝浅拷贝的问题.总结一下~ JS数据存储和深浅拷贝实际运用① 这是之前写过的一篇文章,解决浅拷贝深拷贝的问题只说了一种方法,今天来补充一下. 介绍深拷贝和浅拷贝都在上一篇文 ...
- Delphi代码规范
1. 前言 本文档主要是为Delphi开发人员提供一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致格式可遵循.这样,每个编程人员编写的代码能够被其他人理解. 2. 源程序书写规范 2 ...
- 解决CSocket高数据传输问题
这个是自己项目中发现的问题,所以这个不一定适用于你的. 仅供参考. 头文件: ESSocket.h // ESSocket.h : header file // #ifndef ESSOCKET_H ...
- Oracle时间日期处理方法
https://www.cnblogs.com/plmm/p/7381496.html 1.用于截取年.月.日.时.分.秒 extract()函数 extract(year from sysdate) ...
- numpy.flatnonzero():
numpy.flatnonzero(): 该函数输入一个矩阵,返回扁平化后矩阵中非零元素的位置(index) 这是官方文档给出的用法,非常正规,输入一个矩阵,返回了其中非零元素的位置. 1 >& ...
- bzoj 3029 守卫者的挑战——概率期望dp+状态数思考
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3029 先随便写了个dfs,记录“前 i 次.成功 j 次.容量-残片=k”的概率.因为是否可 ...
- Faster RCNN 的细节补充
一.faster rcnn的结构 通过上面的结构,我们知道该faster rcnn前面以VGG16为框架,加入RPN层,最后做分类层. 采用VGG16相对ZF来说慢一点,但是精度也高一点. 二.RPN ...
- 如何在linux中部署mongodb并设置连接认证
在windows上给mongodb设置连接认证权限:mongodb默认是不认证的,默认没有账号,现在就讲讲怎么设置账户和密码 1.首先进入C:\mongodb\bin下面双击运行mongo.exe启动 ...
- HDU1950
//虽然是一道LIS问题,但是还是第一次用O(n*lgn)这种算法,赶角波错哈哈哈哈....至少今天有所收获 #include<cstdio> #include<cstring> ...