codeforces 442C C. 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 题意: 给出n个数,每次删去一个数,得到的分数是min(a,b),a和b是这个左右相邻的数,边界上的数不能删去,现在要求怎么样才能得到最大的得分; 思路: 贪心的想,每次如果一个数不小于它相邻的两个数,那么这个数就可以删去,得到的分数就是min(a,b),最后剩下的就是一个倒v形状的或者一半,最高的那两个选不了,再把剩下的相加就好了; AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e5+10;
int n,a[maxn],q[maxn]; int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
LL ans=0;
q[1]=a[1];int p=1;
for(int i=2;i<=n;i++)
{
while(q[p]<=a[i]&&q[p]<=q[p-1]&&p>1)
{
ans=ans+min(q[p-1],a[i]);
p--;
}
q[++p]=a[i];
}
sort(q,q+p+1);
for(int i=1;i<p-1;i++)ans=ans+q[i];
cout<<ans<<endl; return 0;
}
codeforces 442C C. Artem and Array(贪心)的更多相关文章
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- [CF442C] Artem and Array (贪心+单调栈优化)
题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- 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
C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
随机推荐
- Ubuntu14.04安装ROOT集群
之前尝试在CentOS7上部署ROOT集群,却发现无论是源码包安装,还是官方提供的二进制包,都缺少了关键的xproofd可执行文件,导致PoD不能运行.没有办法,只能尝试在其他OS上部署,这里我选择了 ...
- Python 学习笔记1
1.Python2.x与3.x版本区别 2.常量与变量 3.if elif else 4.注释 5.用户交互 6.字符串拼接 7.文件扩展名 8.缩进 9.运算符 10.while循环 Pyt ...
- mysql innodb表 utf8 gbk占用空间相同,毁三观
昨天因为发生字符集转换相关错误,今天想验证下utf8和gbk中英文下各自空间的差距.这一测试,绝对毁三观,无论中文还是中文+英文,gbk和utf8占用的实际物理大小完全相同,根本不是理论上所述的“UT ...
- [Xamarin.Android] Support Library Tips
[Xamarin.Android] Support Library Tips Support Library支持内容 Xamarin Support Library每个版本支持.那些组件,可以参考这份 ...
- 字母排序问题(c++实现)
描述:编写一个程序,当输入不超过60个字符组成的英文文字时,计算机将这个句子中的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度 相同.例如: 输入: THE PRICE OFB ...
- JavaScript一个经典问题
看下面代码 <ul id="demo"> <li></li> <li></li> <li></li&g ...
- SharePoint 2010 类似人人网站内信功能实施
简介:用SharePoint代码加实施的方式,完成类似人人网站内信功能,当然,实现的比较简单,样式也比较难看,只为给大家一个实施的简单思路,如有谬误,还请见谅.当然,还有就是截图比较长,当然为了让大家 ...
- Struts2(十一)OGNL标签三与Struts2标签
一.UI标签 二.简单例子 <h1>添加信息</h1> <!--通过指定theme的属性改变主题 --> <s:form action="" ...
- IT技术很好的视频网址
1.华为工程师 带你实战C++ 2.vimoe,国外的,需要FQ哦.https://vimeo.com/85831438
- 在类库项目中使用log4net(RollingFileAppender)记录日志
1.创建解决方案 2.创建类库项目 3.根据需要修改命名空间,修改(和/或)添加类到类库 4.引用log4net 5.类库项目根目录下创建leg4net配置文件,如D3CallTriggerPlugi ...