cf442C Artem and Array
2 seconds
256 megabytes
standard input
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.
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.
5
3 1 5 2 6
11
5
1 2 3 4 5
6
5
1 100 101 100 1
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的更多相关文章
- [CF442C] Artem and Array (贪心+单调栈优化)
题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-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 ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces 442C Artem and Array (看题解)
Artem and Array 经过分析我们能发现, 如果对于一个a[ i ] <= a[ i + 1 ] && a[ i ] <= a[ i - 1 ]可以直接删掉. 最 ...
- 「CF442C」 Artem and Array
题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- Artem and Array
Codeforces Round #253 (Div. 1) C:http://codeforces.com/problemset/problem/442/C 题意:给你一个序列,然后你每次可以删除一 ...
- Artem and Array CodeForces - 442C (贪心)
大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...
- Codeforces Round 253 (Div. 2)
layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...
随机推荐
- windows下定时利用bat脚本实现ftp上传和下载
前言: 工作中可能会遇到以下情况,利用windows作为中转,来实现两台linux服务器的文件传输. 实现步骤: 1.FTP上传和下载的bat脚本. 脚本分为两部分:可执行bat脚本和ftp命令文件: ...
- JS浏览器对象-Screen对象
代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- 把本地建好的项目提交到git上
才开始用git来控制项目版本,刚开始时不是很会用,由于公司最近新开个项目,需要我把建好的项目放到git上去,慢慢的摸索,终于有点小小的结果,把一个项目成功提交到git上了,在这里记录下,以免下次忘记, ...
- Apache+Subversion+TortoiseSVN
Key words: dav_svn, apache, subversion, tortoisesvn # install apache2 sudo apt-get install libapache ...
- 【MongoDB数据库】怎样安装、配置MongoDB
本blog以最简洁的方式记录了博主在折腾MongoDB过程中点点滴滴,当中包含下载MongoDB.配置环境变量.怎样启动MongoDBserver.怎样连接MongoDBserver以及怎样连接Mon ...
- inode-软链接与硬链接
一.inode是什么?理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取硬 ...
- Java多线程练习二
public class ex3 { public static void main(String [] args) { thread2 t1 = new thread2("hello&qu ...
- JS的作用域和作用域链
每个函数都有自己的作用域,当执行流进入一个函数时,函数就会被推入栈中,而在函数执行之后,栈将其执行环境弹出,把控制权放回给之前的作用域,全局作用域是最外围的一个作用域,因此,所有全局变量和函数都是作为 ...
- 2:url有规律的多页面爬取
举例网站:http://www.luoo.net/music/期刊号 e.g:http://www.luoo.net/music/760 打算爬取其title:Hello World:pic:desc ...
- js动态加载html,加载后的页面元素某些事件失效的解决方案
用 live 来绑定 例如: $("#items li .addToCartimg").live("click",function(){ $('.popDeta ...